Scope UIView animations to specific views or properties

animation, ios, objective-c, uiview

Solution

Take a look at `+[UIView performWithoutAnimation:]`. You specify a block of changes you wish to perform without animation and they happen immediately.

This is good for iOS7 and above, and only for UIKit animation. For dealing with animations on layer objects directly, or support for older versions of iOS, you can use the following code:

[CATransaction begin];
[CATransaction setDisableActions:YES];
//Perform any changes that you do not want to be animated
[CATransaction commit];

More on `performWithoutAnimation:` here and on `setDisabledActions:` here.

If you do not wish to alter the parent's code, you can implement the setter methods of the properties you do not wish animated, and wrap the super call with `performWithoutAnimation:`, like so:

- (void)setFrame:(CGRect)frame
{
    [UIView performWithoutAnimation: ^ { 
        [super setFrame:frame]; 
    }];
}

Problem

I have a UIView with subviews and want to animate only specific properties of certain views. For example, I sometimes want to call `[self layoutIfNeeded]` and animate only the bounds but not other properties of the view or its subviews. The problem is that `+[UIView animateWithDuration:animations]` tracks subviews and all animatable properties. Is there a reasonable solution to this?

Original source