Jaa*_*aay 2 core-animation ios
在我的iPhone应用程序中,我有一个UIButton,当我按下另一个UIButton时,我旋转180度进入视图,然后再次点击时按钮再旋转180度回到它开始的位置.
这一切都在第一次完成360度全过程时工作正常,但是如果我尝试从头开始再次开始它会弹开180度然后尝试从那一点旋转它.谁能指出我正确的方向?这是我到目前为止的代码......
showAnimation= [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
showAnimation.duration = self.showAnimationDuration;
showAnimation.repeatCount = 1;
showAnimation.fillMode = kCAFillModeForwards;
showAnimation.removedOnCompletion = NO;
showAnimation.cumulative = YES;
showAnimation.delegate = self;
float currentAngle =[[[rotateMe.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];
//Rotate 180 degrees from current rotation
showAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:currentAngle],
[NSNumber numberWithFloat:currentAngle + (0.5 * M_PI)],
[NSNumber numberWithFloat:currentAngle + M_PI], nil];
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
Run Code Online (Sandbox Code Playgroud)
完成动画后,我将rotateMe.transform旋转更新为图层的旋转,以使其可用.
- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
float currentAngle =[[[self.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];
NSLog(@"End: %f", currentAngle);
rotateMe.transform = CGAffineTransformMakeRotation(0);
}
Run Code Online (Sandbox Code Playgroud)
我已经取得了充分的工作效果
[UIView animateWithDuration:1.0f]
animations:^{
CGAffineTransform transform = CGAffineTransformRotate(rotateMe.transform, DEGREES_TO_RADIANS(179.999f));
rotateMe.transform = transform;
}
];
Run Code Online (Sandbox Code Playgroud)
但我想使动画更复杂,因此CAKeyframeAnimation.
additive如果需要所有关键帧,可以将动画配置为0到180度的动画.如果您不需要不同的关键帧,您只需使用基本动画和byValue属性即可.下次添加动画时,它会比视图上的任何旋转旋转180度.
如果要在委托回调中设置实际值,则不需要填充模式,也不需要在完成时删除动画.
CABasicAnimation *showAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.byValue = M_PI;
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
Run Code Online (Sandbox Code Playgroud)
或使用关键帧动画(如上所述:使其成为0至180度的加法和动画).
CAKeyframeAnimation *showAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.additive = YES; // Make the values relative to the current value
showAnimation.values = @[0, /*all your intermediate values here... ,*/ M_PI];
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3463 次 |
| 最近记录: |