garethdn
garethdn

Reputation: 12351

Unusual animateWithDuration behaviour in viewDidLoad

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?

Upvotes: 1

Views: 734

Answers (2)

Sulthan
Sulthan

Reputation: 130102

You never need to call it in viewDidLoad. That's simply wrong and it won't work. Move the code into viewDidAppear. If you have reasons to put it into viewDidLoad, fix the reasons!

EDIT: You never know when viewDidLoad is 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 a UINavigationController, your animation will collide with the "push" animation and they will be slow. That's why you are supposed to use viewDidAppear because when this method is called you know that the controller is displayed and the "appear" animation has ended.

Upvotes: 7

Jegadesh
Jegadesh

Reputation: 1

Call the method using some delay.

[self performSelector:@selector(shakeAnimation) withObject: afterDelay:2.0]

Upvotes: 0

Related Questions