Search code examples
objective-ciosuiviewanimationcgaffinetransform

Scaling UIView using transform after animating frame change causes UIView to jump back to original frame before scaling


I'm trying to scale a UIView (with animation) after I move it (with animation). The problem is, when the scaling animation begins, it jumps back to the original position. Why?

[UIView animateWithDuration:t delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    // Drop the ball

    CGRect frame = coinView.frame;
    frame.origin.y += d;

    coinView.frame = frame;

    coinView.shouldSparkle = NO;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
        // Initial scale up for ball "poof"

        coinView.transform = CGAffineTransformScale(coinView.transform, 1.5, 1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            coinView.transform = CGAffineTransformScale(coinView.transform, 0.000001, 0.000001);
        } completion:^(BOOL finished) {
            [coinView removeFromSuperview];
        }];
    }];
}];

EDIT: This is how I generated my d:

static CGFloat groundPositionY = 325;

CGRect convertedFrame = [coinView.superview convertRect:coinView.frame toView:self.view];

CGFloat d = groundPositionY - CGRectGetMaxY(convertedFrame);

EDIT2: Okay, so I changed the second UIView animation to the following and I discovered that the jump (and the scale down) happens before the second animation occurs, i.e. when animateWithDuration:delay:options:animations:completion: is called.

[UIView animateWithDuration:5 delay:3 options:0 animations:^{
    coinView.transform = CGAffineTransformScale(coinView.transform, 1.5, 1.5);
} completion:nil];

Solution

  • I found the problem...

    The coinView is a subview of a child view controller's view. The problem comes from the fact that I overrode that child view controller's viewDidLayoutSubviews method to lay out all the coinView's, so whenever that method was called, the coin view would move back to its original position and size intended by the child view controller.

    Thanks for all of your help, repoguy and sergio!!