我创建了以下keyFrame动画;在这种情况下,常数为UIView的CGPoint x坐标(此动画可以使对象上下移动,重复执行,并且可以平滑地进行移动。
当我将totalDuration设置为时2.0
,它可以很好地工作,但是当我将totalDuration设置为1.5
或时1.6
,它会变得非常紧张-好像UIView没有时间返回到动画再次开始之前的原始位置。
显然我的数学在这里是错误的,但我只是不能将手指放在哪里。
- (void)linearAnimation:(CGFloat)constant imageView:(UIImageView*)imageView animationStyle:(RSGesturesTutorialAnimationStyle)animationStyle{
CGFloat frameDuration;
CGFloat totalDuration = 1.6; // setting this to 2.0 animates smoothly, 1.5/6 do not.
NSArray* keyFrames = @[
[NSNumber numberWithFloat:constant - 100],
[NSNumber numberWithFloat:constant],
[NSNumber numberWithFloat:constant + 100],
[NSNumber numberWithFloat:constant]
];
frameDuration = totalDuration/keyFrames.count;
[UIView animateKeyframesWithDuration:totalDuration
delay:0
options: UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionRepeat
animations:^{
[keyFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[UIView addKeyframeWithRelativeStartTime:idx*frameDuration relativeDuration:frameDuration animations:^{
imageView.center = CGPointMake(imageView.center.x, [obj floatValue]);
}];
}];
} completion:^(BOOL finished) {
}];
}
这样的事情应该起作用:
- (void)linearAnimation:(CGFloat)constant imageView:(UIImageView *)imageView {
NSTimeInterval totalDuration = 1.6; // setting this to 2.0 animates smoothly, 1.5/6 do not.
CGFloat startY = imageView.center.y;
NSArray* keyFrames = @[
@(startY - constant),
@(startY),
@(startY + constant),
@(startY)
];
CGFloat period = 1.0f / keyFrames.count;
[UIView animateKeyframesWithDuration:totalDuration
delay:0
options: UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionRepeat
animations:^{
[keyFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSTimeInterval interval = idx * period;
[UIView addKeyframeWithRelativeStartTime:interval relativeDuration:period animations:^{
imageView.center = CGPointMake(imageView.center.x, [obj floatValue]);
}];
}];
} completion:^(BOOL finished) {
}];
}
将关键帧添加到动画时,relativeStartTime
和relativeDuration
值不是绝对值,它们必须是整个动画的百分比。这样一来,您可以更改totalDuration,而不必更改许多持续时间值。
从文档:(frameStartTime) The time at which to start the specified animations. This value must be in the range 0 to 1, where 0 represents the start of the overall animation and 1 represents the end of the overall animation. For example, for an animation that is two seconds in duration, specifying a start time of 0.5 causes the animations to begin executing one second after the start of the overall animation.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句