简体   繁体   中英

Synchronize disabling of animation in iOS?

I use some various blocks which looks like the following example:

        [UIView setAnimationsEnabled:NO];
        ...
        [UIView setAnimationsEnabled:YES];

As I understand they may be called in a background thread and I should prevent mixing of disable/enable commands. How to do it correctly?

UIKit operations cannot be done on background threads. They can be done only on main thread. ie animations can be enabled/disabled only on main thread. Can you be a bit more specific about the use case?

  • (void)setAnimationsEnabled:(BOOL)enabled Description Sets whether animations are enabled. Animations are enabled by default. If you disable animations, code inside subsequent animation blocks is still executed but no animations actually occur. Thus, any changes you make inside an animation block are reflected immediately instead of being animated. This is true whether you use the block-based animation methods or the begin/commit animation methods. This method affects only those animations that are submitted after it is called. If you call this method while existing animations are running, those animations continue running until they reach their natural end point.

The documentation for setAnimationsEnabled doesn't say anything about background thread. In fact, all UI operations in iOS must be done in main thread.

So responding your question, the only thing you have to be careful with is calling the setAnimatinosEnabled method in the main thread.

If you want to perform different block of animations at different moments, its easier to use:

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM