Dim*_*lov 4 animation objective-c uiview ios autolayout
我有UIView,我想用旋转动画它.我也想使用autolayout.我已经看到了很多stackoverflow常见问题,但我没有找到任何适用的解决方案.那么,让我们从我的界面和动画代码开始吧; 我有一个UIView图像,必须旋转.现在我有按钮,激活旋转.在屏幕截图中你可以看到红十字,这必须是旋转的中心(现在它在图像上,但我想在旋转的UIView之外设置旋转中心,我知道这可以用AnchorPoint存档).

这是我的旋转动画代码:
#define ROTATE_DURATION 3.0
- (IBAction)rotateArrow {
CGAffineTransform transform = self.hand.transform;
NSLog(@"layer possition before animation x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
self.hand.transform = CGAffineTransformRotate(transform, 2*M_PI/3) ;
[self.hand layoutIfNeeded];
NSLog(@"layer possition after animation 1 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
self.hand.transform = CGAffineTransformRotate(transform, -2*M_PI/3) ;
[self.hand layoutIfNeeded];
NSLog(@"layer possition after animation 2 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
self.hand.transform = CGAffineTransformRotate(transform, 0) ;
[self.hand layoutIfNeeded];
NSLog(@"layer possition after animation 3 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {
}];
}];
}];
}
Run Code Online (Sandbox Code Playgroud)
那么,是什么问题:当转动过程中的UIView改变它center和layer.position属性,这就是为什么在设置动画时我UIView是"跳跃".如果关闭自动布局,动画就可以了.我观看了WWDC 2012"按示例自动布局",发现我会使用[self.hand layoutIfNeeded];一切都很好,但它根本没有.动画变得更平滑,但我看到这个"跳跃".所以,这是我的"输出".

当它是动画时,UIView向右移动,就像你在图像上看到的那样,而不是回到正常位置.我怎样才能解决这个"跳跃"问题?
这是一个日志:
layer possition before animation x: 160.000000; y: 99.500000
layer possition after animation 1 x: 197.349030; y: 114.309601
layer possition after animation 2 x: 197.349030; y: 114.309601
layer possition after animation 3 x: 160.000000; y: 99.500000
Run Code Online (Sandbox Code Playgroud)
谢谢.
当我布置我的视图时,一切都开始正常工作.进行转换时,重要的是要记住,自动布局总是根据您设置的约束计算视图边界和框架.因此,在我的情况下,我只是将中心垂直和水平对齐,宽度和高度添加到我的旋转视图,因此自动布局机制知道我的视图到底是什么时候.一切都很顺利.这是一个很好的自动布局教程:
http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-1