我正在为一个时钟臂设置动画,使其从指向 12 点钟指向当前时间。如果说,11 点钟,我想让 ARM 顺时针旋转到 11 点钟的位置。但当然如果我使用:
CGAffineTransform rotation = CGAffineTransformMakeRotation(2*M_PI*11/12);
[UIView animateWithDuration:3.0
animations:^{
clockArm.transform = rotation;
}];
逆时针旋转。我试过:
CGFloat angle = 2*M_PI*11/12;
CGAffineTransform firstRotation = CGAffineTransformMakeRotation(M_PI-0.001);
CGFloat firstRotationTime = 3.0*(M_PI/angle);
CGAffineTransform secondRotation = CGAffineTransformMakeRotation(angle);
CGFloat secondRotationTime = 3.0 - firstRotationTime;
[UIView animateWithDuration:firstRotationTime
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.clockArm1.transform = firstRotation;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:secondRotationTime
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.clockArm1.transform = secondRotation;
}
completion:^(BOOL finished){
}];
}];
动画确实是顺时针方向进行的,但它是断断续续的——动画似乎仍然在为第一个动画执行 UIViewAnimationEaseInOut。我做错了什么,或者是否有其他方法可以实现我想要的?
最佳答案
您可以使用 CATransaction
的完成 block 来设置动画完成时 View 的旋转属性。以下函数在我的测试用例中有效:
- (void) rotateViewAnimated:(UIView*)view
withDuration:(CFTimeInterval)duration
byAngle:(CGFloat)angle
{
[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:angle];
rotationAnimation.duration = duration;
rotationAnimation.removedOnCompletion = YES;
[CATransaction setCompletionBlock:^{
view.transform = CGAffineTransformRotate(view.transform, angle);
}];
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];
}
你喜欢用它
[self rotateViewAnimated:self.clockArm1 withDuration:3.0 byAngle:2*M_PI*11./12.];
关于iphone - 顺时针旋转 UIView 大于 180 度的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825356/