用CGAffineTransformIdentity逆时针转为顺时针方向
问题描述:
我有一个电话轮。在发言中,它以动画的形式出现在他的位置上。用CGAffineTransformIdentity逆时针转为顺时针方向
直到角度小于180°,它会顺时针返回。没问题,用此代码:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [UIView setAnimationBeginsFromCurrentState:YES]; [UIView beginAnimations:nil context:NULL]; [UIViewsetAnimationDuration:0.5]; wheel.transform = CGAffineTransformIdentity; [UIView commitAnimations]; }
但它出错后继续旋转完成转。
我试图做出动画像这样:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [UIView setAnimationBeginsFromCurrentState:YES]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.2]; wheel.transform = CGAffineTransformRotate(wheel.transform, degreesToRadians(-130)); [UIView commitAnimations]; [self performSelector:@selector(animatewheelViewToCenter) withObject:nil afterDelay:0.3]; } - (void)animatewheelViewToCenter{ [UIView setAnimationBeginsFromCurrentState:YES]; [UIView beginAnimations:nil context:NULL]; [UIViewsetAnimationDuration:0.3]; wheel.transform = CGAffineTransformIdentity; [UIView commitAnimations]; }
它的工作原理,但动画不是流体;该变化是可见的。
答
我不确定touchesEnded(根据旋转角度)是什么状态,而我正在琢磨你选择degreesToRadians(-130)来尝试部分地完成它,并期望下一个方法完成剩下的工作。这应该是一个更好的解决方案,希望能产生你期待的结果。我不确定什么是卡特兰,为什么你在旋转,所以我只是旋转轮子。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformMakeRotation(degreesToRadians(-130));
// I would recommend that the degrees you rotate be half of whatever your rotation is.
// this would make the wheel rotate to the home position more evenly
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animatewheelViewToCenter:finished:context:)];
[UIView commitAnimations];
}
- (void)animatewheelViewToCenter:(NSString *)animationID finished:(NSNumber *)finished context:(id)context {
[UIView setAnimationBeginsFromCurrentState:YES]; // you might need to make this NO
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.2];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
编辑:其实,我很可能会令旋转(在本例-130度)比一半会略多,因为CGAffineTransformIdentity是要去走最短路径回到正规,所以如果你完全按照1/2的方式走,它可能不会沿着你想要的正确方向(顺时针或逆时针)。
这是正确的,操作系统为此执行操作的方式是错误的,动画将最短路径旋转回缩进,这就是为什么您将问题解决的问题 – Daniel 2009-08-04 23:44:51