UIView动画块和dispatch

UIView动画块和dispatch

本文介绍了理解ios UIView动画块和dispatch_async(dispatch_get_main_queue)块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地理解这个主题.假设我想做一些非常酷的动画,如下所示

I am trying to get a better understanding of this topic. Lets say I want to do some really cool animation like the following

- (void)coolAnimation
{
    [UIView animateWithDuration: some duration
                     animations:^{ some animation }];
}

因为它是一个动画块,它会自动添加到 main_queue 吗?或者为了最佳实践,我应该始终将 UI 更新添加到 main_queue 中,如下所示.

Since its an animation block, does it automatically added to the main_queue ? Or for best practice, I should always add the UI updates into the main_queue like the following.

dispatch_async(dispatch_get_main_queue(), ^{
    [self coolAnimation];
});

推荐答案

无论您在何处调用 [UIView animateWithDuration:animations:],块的内容都在主线程上执行.最好让操作系统运行您的动画;动画线程不会阻塞主线程——只阻塞动画本身.

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.

在一个非常高级的手动波形视图中,[UIView animateWithDuration:animations:] 评估您的块并确定其中每个可动画值的函数.操作系统有一个动画线程,它用作它的帧计时器,然后在每次滴答时,它会在主线程上将更新发送到您的动画属性及其进度值.(您可以在 PresentationLayer 上检查这些值的当前状态.)

In a very high-level hand-wavy kind of view of this, [UIView animateWithDuration:animations:] evaluates your block and determines functions for each animatable value within it. The OS has an animation thread that it uses as its frame timer, then at each tick it sends an update on the main thread to your animatable properties with their progressing values. (You can check the current state of these values on your presentationLayer.)

这篇关于理解ios UIView动画块和dispatch_async(dispatch_get_main_queue)块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:46