Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation block: if duration is 0, is it the same as without animation?

I need to handle a case where you can do something with or without animation, instead of:

if (animation)
{
    [UIView animateWithBlock:^(){...}];
}
else
{
    ...
}

I want to do:

[UIView animateWithBlock:^(){...} duration:(animation ? duration : 0)]

but not sure if it works, even if it does, is there any overhead for using this instead of directly change the view?

Thanks

like image 295
hzxu Avatar asked Nov 13 '12 22:11

hzxu


People also ask

How does UIView animate work?

it queries the views objects for changed state and gets back the initial and target values and hence knows what properties to change and in what range to perform the changes. it calculates the intermediate frames, based on the duration and initial/target values, and fires the animation.

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

What is animated in Swift?

Animations can add visual cues that notify users about what's going on in the app. In iOS, animations are used extensively to reposition views, change their size, remove them from view hierarchies, and hide them.


2 Answers

What I do in this cases, is to create a block that contains all the animations I want to make. Then execute an UIView animation passing the animation block as a parameter, or directly calling that block, whether I want it to be animated or not. Something like this :

void (^animationBlock)();
animationBlock=^{
      // Your animation code goes here
};
if (animated) {
    [UIView animateWithDuration:0.3 animations:animationBlock completion:^(BOOL finished) {

    }];
}else{
    animationBlock();
}

That would avoid the overhead

like image 76
Daniel García Avatar answered Sep 19 '22 13:09

Daniel García


According to the Apple docs:

If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle.

like image 44
chebur Avatar answered Sep 19 '22 13:09

chebur