Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope UIView animations to specific views or properties

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?

like image 907
ide Avatar asked Mar 25 '14 06:03

ide


1 Answers

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]; 
    }];
}
like image 125
Léo Natan Avatar answered Sep 21 '22 17:09

Léo Natan