如何使用CAKeyFrameAnimation向后动画图像

时间:2013-06-20 07:03:01

标签: ios objective-c cakeyframeanimation

我是iOS开发的新手。我正在创建一个用于旋转图像的关键帧动画。图像已成功向前旋转。我想要完全向后旋转图像。

这是我的代码。

UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(28,158,133,133)];
tempView.image = [UIImage imageNamed:@"1_03.png"];
[self.view addSubview:tempView];

const NSUInteger rotations = 1;
const NSTimeInterval duration  = 4.0f;

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle + angularVelocity * duration)];
anim.duration = duration;
anim.delegate = self;
anim.repeatCount = INFINITY;
[tempView.layer addAnimation:anim forKey:@"animation"];

tempView.transform = CGAffineTransformMakeRotation(touchUpStartAngle +     (touchUpEndAngle));

如何做到这一点。

2 个答案:

答案 0 :(得分:3)

试试这个。

UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(28,158,133,133)];
tempView.image = [UIImage imageNamed:@"1_03.png"];
[self.view addSubview:tempView];

const NSUInteger rotations = 1;
const NSTimeInterval duration  = 4.0f;

CAKeyframeAnimation *anim = [CAKeyframeAnimation      animationWithKeyPath:@"transform.rotation"];
CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle - angularVelocity * duration)];
anim.duration = duration;
anim.delegate = self;
anim.repeatCount = INFINITY;
[tempView.layer addAnimation:anim forKey:@"animation"];

tempView.transform = CGAffineTransformMakeRotation(touchUpStartAngle +     (touchUpEndAngle));

答案 1 :(得分:1)

试试这个

CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle - angularVelocity * duration)];
相关问题