Search code examples
iosuiviewcgaffinetransformcatransform3d

Rotating a UIView before animation happens


I'd like to rotate an instance of my UIView subclass before scaling it with an animation. Unfortunately the code I have rotates and scales in the same animation. How can I complete or force the rotation before any scaling animation happens?

- (void) layoutSubviews {
    self.transform = CGAffineTransformMakeRotation(myAngle);

    // other layout...
}

- (void) showMyView {
    [UIView setAnimationCurve:something];
    [UIView setAnimationDuration:somethingElse];

    self.layer.transform = CATransform3DMakeScale(x, y, z);

    [UIView commitAnimations];
}

Solution

  • First of all, it is recommended by apple to use the Block syntax for animation stuff.
    In addition to that it makes it easier to achieve what you want.

    First make the animation for your rotation and if it's done make the scaling stuff.
    Code example:

        [UIView animateWithDuration:0.5 
                     animations:^{
                         // This rotates the layer by 90°
                         self.layer.transform = CGAffineTransformMakeRotation(M_PI/2.0);
                     }
                     // On completition start the scaling
                     completion:^(BOOL finished){
                         if (finished) {
                             [UIView animateWithDuration:0.5 
                                              animations:^{
                                                  // This scales
                                                  self.layer.transform = CGAffineTransformMakeScale(2.0, 2.0);
                                              }
                              ];
                         }
                     }
        ];  
    

    It is also possible with the "old" animation style you use, but it's more complicated to implement the animation delegate etc...