同步禁用 iOS 中的动画?
Synchronize disabling of animation in iOS?
我使用了一些不同的块,如下例所示:
[UIView setAnimationsEnabled:NO];
...
[UIView setAnimationsEnabled:YES];
据我所知,它们可能会在后台线程中调用,我应该防止混合使用 disable/enable 命令。如何正确操作?
UIKit 操作不能在后台线程上完成。它们只能在主线程上完成。即动画只能在主线程上 enabled/disabled。您能否更具体地说明用例?
- (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.
setAnimationsEnabled 的文档没有说明任何关于后台线程的内容。事实上,iOS中的所有UI操作都必须在主线程中完成。
所以回答你的问题,你唯一需要注意的是在主线程中调用 setAnimatinosEnabled 方法。
如果你想在不同的时刻执行不同的动画块,它更容易使用:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
我使用了一些不同的块,如下例所示:
[UIView setAnimationsEnabled:NO];
...
[UIView setAnimationsEnabled:YES];
据我所知,它们可能会在后台线程中调用,我应该防止混合使用 disable/enable 命令。如何正确操作?
UIKit 操作不能在后台线程上完成。它们只能在主线程上完成。即动画只能在主线程上 enabled/disabled。您能否更具体地说明用例?
- (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.
setAnimationsEnabled 的文档没有说明任何关于后台线程的内容。事实上,iOS中的所有UI操作都必须在主线程中完成。
所以回答你的问题,你唯一需要注意的是在主线程中调用 setAnimatinosEnabled 方法。
如果你想在不同的时刻执行不同的动画块,它更容易使用:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0