an0
an0

Reputation: 17530

How to resize a view mid-way during a moving animation?

During a sliding animation(down, pause, then up back to the original position) of a subview, the device is rotated, so the superview is rotated. I want to keep the subview's width the same as the superview, so I need to resize it during its sliding animation.

Here is the sliding animation code:

[UIView animateWithDuration:0.3 animations:^{
    self.frame = finalFrame;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 delay:3 options:0 animations:^{
        self.frame = initFrame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}];

This is the method that is called when I detect rotation:

- (void)rotate:(NSNotification *)notif {
    // What to do here to adjust the width but also keep the sliding animation going.
}

Upvotes: 1

Views: 153

Answers (2)

an0
an0

Reputation: 17530

It seems there is no auto-resizing magic that can be used here. One must:

  1. Record the progress of the animations.
  2. On detection of rotations, cancel the old animations, adjust the view size, and add new animations starting from the current progress.

Here is a sample project for reference: http://d.pr/f/M4UW.

Upvotes: 1

David Rönnqvist
David Rönnqvist

Reputation: 56625

You can animate the bounds of the layer to change the width. Just make the height the same and apply an animation for the bounds.

If you want both animations to have the same duration, timing function etc. then you could add them both to an animation group and add that group to the layer you are animating.

Upvotes: 0

Related Questions