0

我想在动画结束时减慢动画速度。
我正在浏览这段代码。

[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];

但是当它即将结束或在完成块时无法减速。

4

2 回答 2

1

尝试使用kCAMediaTimingFunctionEaseOut而不是kCAMediaTimingFunctionEaseIn

rotationAnimation.timingFunction = [CAMediaTimingFunction
                                    functionWithName:kCAMediaTimingFunctionEaseOut];

kCAMediaTimingFunctionEaseIn 指定缓入节奏。Ease-in pacing 使动画开始缓慢,然后随着进度加快。

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

于 2015-01-06T14:35:20.757 回答
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];.

于 2015-01-07T12:23:57.003 回答