Search code examples
iosanimationuiviewcgaffinetransform

CGAffineTransformMakeTranslation vs. CGRectOffset Animation


Not sure if this is relevant to my question, but I have an animation sequence that moves a UIView out of frame with the following code:

[UIView animateWithDuration:0.3 
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseInOut 
                 animations:^{
                     _popupView.transform = CGAffineTransformMakeTranslation(0, -750);
                 } 
                 completion:^(BOOL finished) { 
                     _popupView.alpha = 0.0f;
}];

I had previously been using this INSTEAD of the above animation

                 ....
                 animations:^{
                     _popupView.frame = CGRectOffset(_popupView.frame, 0, -750);
                 } 

I had been getting some strange glitchy errors when performing the CGRectOffset for whatever reason, but as soon as I replaced the method of moving the view with the CGAffineTransfromMakeTranslation the glitch seems to have gone away.

My question is this:

  • What is the end result difference (if any at all) between these two methods of animating ("moving") the location of an object?

If there is, could that explain the reason I was experiencing the view flashing at the bottom of the screen during the animation (almost like it was setting its frame to the bottom of the screen). If this isn't quite clear, I have a separate post that asks more specifically about this "glitch" with a more in-depth explanation about my CGRectOffset Glitch.

Thanks for any help!


Solution

  • The difference is that auto layout expects to have complete control of your view's center and bounds.size, and will reset them according to the installed constraints at arbitrary times.

    Auto layout is careful (as of iOS 8.0) to avoid even looking at your view's transform. Your transform is applied on top of the frame set by auto layout, and auto layout never resets it.

    So, you can animate your view's position using the transform, or by updating its constraints. Trying to animate its position by setting its frame directly leads to unexpected behavior.