I’m experiencing some unusual behaviour when performing an animated transform on a UIImageView. The code in the method below makes the image appear like it’s rocking from side-to-side:
-(void) shakeAnimation
{
float degrees = 30; //the value in degrees
imgShake.autoresizingMask = UIViewAutoresizingNone;
[UIView animateWithDuration:0.20f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
imgShake.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);
} completion:^(BOOL finished) {
imgShake.transform = CGAffineTransformMakeRotation(1*M_PI/180);
NSLog(@"Shake finished");
}];
}
The problem comes with where i call the method. If i call the method in viewDidAppear the animation seems to work perfectly…but for other reasons i need to call it in viewDidLoad. When i call the method from viewDidLoad the animation functions but not at the speed specified by animateWithDuration. It’s much slower, probably 0.70f. Is there something i could be missing here?
You never need to call it in
viewDidLoad. That’s simply wrong and it won’t work. Move the code intoviewDidAppear. If you have reasons to put it intoviewDidLoad, fix the reasons!EDIT: You never know when
viewDidLoadis called – it can even be called multiple times for one controller. Usually the problem with slow animations is caused by a collision between two animations. For example, if your controller is animated to the screen by aUINavigationController, your animation will collide with the “push” animation and they will be slow. That’s why you are supposed to useviewDidAppearbecause when this method is called you know that the controller is displayed and the “appear” animation has ended.