Search code examples
iphoneipadanimationioscore-animation

animation speed is different between beginAnimations and using animations block


I am trying to convert some animation code that uses beginAnimations to use animation blocks. The problem that I am having is that when I use the animations blocks the animations goes much faster and I cannot seem to adjust the speed by changing the duration value

This is what I started with originally:

birdFly.image = [UIImage imageNamed: @"bird1.png"];
[self.view addSubview:birdFly];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:20];
birdFly.frame = CGRectMake(-1116,27,111,48);
[UIView commitAnimations];
[birdFly release];

This is what I tried first to replace what I had above. Using this code the animation is much faster than what it was using the original code. Even changing the duration does not seem to change the speed at all:

birdFly.image = [UIImage imageNamed: @"bird1.png"];
[self.view addSubview:birdFly];
[UIView animateWithDuration:20
    animations:^{
        birdFly.frame = CGRectMake(-1116,27,111,48);
    }];

I also tried this, thinking I needed a completion block, but the speed was still just as fast compared with what I had originally when I wasn't using blocks.

birdFly.image = [UIImage imageNamed: @"bird1.png"];
[self.view addSubview:birdFly];     
UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
        [UIView animateWithDuration:20 delay:0.0 options:options animations:^
        {
            birdFly.frame = CGRectMake(-1116,27,111,48);
        } completion:nil];

Is the code all doing the same thing? Am I just missing something very simple here? I thought increasing the duration would make the bird go slower and decreasing it would make it go faster, but it doesn't seem to have any impact t all when change it using blocks.

Any guidance would be appreciated.

Thanks.

Aaron


Solution

  • If your animation block is occurring within another animation block, -animateWithDuration:animations: will inherit the duration of the outer block. Try adding UIViewAnimationOptionOverrideInheritedDuration to the options list (and possibly UIViewAnimationOptionOverrideInheritedCurve if you need to override the curve too).