无法减慢ios中的动画速度

时间:2015-01-06 14:31:07

标签: ios core-animation cabasicanimation catransaction

我希望在它结束时减慢动画速度。 我正在阅读这段代码。

[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:20];
rotationAnimation.duration = 2;
rotationAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[CATransaction setCompletionBlock:^{
    imageView.transform = CGAffineTransformRotate(imageView.transform, DEGREES_TO_RADIANS(myAngle*32.72));
}];

[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

但是当它结束或完成时无法减速。

2 个答案:

答案 0 :(得分:1)

尝试使用kCAMediaTimingFunctionEaseOut代替kCAMediaTimingFunctionEaseIn

rotationAnimation.timingFunction = [CAMediaTimingFunction
                                    functionWithName:kCAMediaTimingFunctionEaseOut];

kCAMediaTimingFunctionEaseIn 指定轻松调整步调。缓入调步使动画开始缓慢,然后随着进展而加速。

kCAMediaTimingFunctionEaseOut 指定缓出调步。缓出式调步会导致动画快速开始,然后在完成时变慢。

U也可以更改rotationAnimation.duration以完全减慢动画速度。

 rotationAnimation.duration = 10 

将使动画运行10秒。

来源:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CAMediaTimingFunction_class/index.html#//apple_ref/doc/constant_group/Predefined_Timing_Functions

您还可以通过以下链接了解有关缓和功能及其行为方式的更多信息。 http://easings.net

答案 1 :(得分:0)

正如我在@rakeshbs的评论中提到的,我想以特定的角度停止动画,所以我通过以下方式修改了这段代码。

[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:20];
rotationAnimation.duration = 2;
rotationAnimation.fromValue = 0;
rotationAnimation.toValue = [NSNumber numberWithFloat:SomeValue];
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
[ImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

通过此代码,我可以通过fromValue停在特定位置,并通过[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

控制开始和结束的速度

在动画中间,如果我想提高动画的速度,我需要增加rotationAnimation.byValue的值,如果降低速度,我需要减少相同的值。

在这里我想提一下,如果我想立即旋转MyImageView 360度,我需要设置rotationAnimation.toValue = [NSNumber numberWithFloat:6.25];