I have UIView, I want to animate it with rotation. I want to use autolayout too. I already saw a lot of stackoverflow common questions, but I didn't find any applicable solution. So, let's start with my interface and animation code; I have a UIView with image, which must be rotated. And right now I have button, that activates rotation. In the screenshot you can see red cross, this is must be the center of rotation (right now it's on the image, but I want to make center of rotation outside of the rotated UIView, I know that this could be archived with AnchorPoint).
This is my rotation animation code:
#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) {
}];
}];
}];
}
So, what's the problem:
When rotation is in progress UIView changes it's center
and layer.position
property, that's why my UIView is "jumps" when animated. If autolayout is turned off, animation is okay. I watched WWDC 2012 "Auto Layout by Example" and found that is I would use [self.hand layoutIfNeeded];
all be just fine, but it isn't at all. Animation became a little smoother, but I see this "jumps". So, here is my "output".
When it's animating, UIView goes to right as you can see on image, and than backs to normal position. How can I fix this "jumps"?
Here is a log:
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
Thanks.