在我的 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"];
动画完成后,我将 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);
}
我已经达到了完全的工作效果
[UIView animateWithDuration:1.0f]
animations:^{
CGAffineTransform transform = CGAffineTransformRotate(rotateMe.transform, DEGREES_TO_RADIANS(179.999f));
rotateMe.transform = transform;
}
];
但我想让动画更复杂,因此 CAKeyframeAnimation。