Edward Dale
Edward Dale

Reputation: 30133

Setting a property twice while animating

What happens if I set a property twice to two different values inside of an animation block? If I execute the following pseudocode:

myView.frame = CGRectMake(0,  0, 50, 50); // state 0
[UIView beginAnimations:@"showBanner" context:NULL];
{
    [UIView setAnimationDuration:2];
    myView.frame = CGRectMake(0, 20, 50, 50); // state 1
    myView.frame = CGRectMake(0, 10, 50, 50); // state 2
}
[UIView commitAnimations];

which of the following results should I get?

  1. The frame animates from state 0 through state 1 to state 2.
  2. The frame animates from state 0 directly to state 2, ignoring state 1.

I would expect result #2 to happen because I would think that the state of the properties is recorded when the animation is committed. I'm getting a behavior in my app which seems to indicate that result #1 is happening, hence my question.

Upvotes: 1

Views: 198

Answers (3)

danh
danh

Reputation: 62676

I needed an answer to this question and didn't find either one given here satisfactory. I built a quick test that placed a small subview like this:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
    self.testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.testView];
}

And then provided two alternative animations like this:

- (void)animateWithSimpleBlock {

    [UIView animateWithDuration:2 animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

- (void)animateWithQualifiedBlock {

    [UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

Here's what happens: on animateWithSimpleBlock, the testView jumps in a single frame (no animation) from it's initial 0,0 position to the intermediate position 200,200. From there, it animates over 2sec to the final 0,300 position.

But animateWithQualifiedBlock animates the testView smoothly from it's initial 0,0 position to the final 0,300 position. This is the behavior I need (b/c I'm building up new view positions with looping methods called in the animation block).

Upvotes: 0

cduhn
cduhn

Reputation: 17918

Actually the answer is 3. None of the above.

The animation block will only animate the last state change for the property. So in your case the frame will animate from state 1 to state 2.

Upvotes: 1

Felix
Felix

Reputation: 35384

(2) The frame animates from state 0 directly to state 2, ignoring state 1.

Upvotes: 1

Related Questions