在iOS4 +中明确禁用UIView动画

P i*_*P i 12 animation explicit ios

我一直在读,Apple建议使用基于块的动画而不是CATransaction

以前,我使用此代码禁用动画:

[CATransaction begin];
[CATransaction setDisableActions: YES];
// !!! resize
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)

是否有新的推荐方法来执行此操作,或者这仍然可以吗?

Jos*_*erg 31

[UIView setAnimationsEnabled:NO];
//animate here
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

  • 使用`UIView`方法的问题在于你不能像使用`CATransaction`方法那样嵌套它.因此,如果您有一段禁用动画的代码并调用其他禁用动画的内容_inside_阻止动画将在块结束前重新启用. (2认同)

Bre*_*nes 25

对于iOS 7及更高版本,现在可以通过以下方式实现:

[UIView performWithoutAnimation:^{
    // Changes we don't want animated here
    view.alpha = 0.0;
}];
Run Code Online (Sandbox Code Playgroud)

  • @GregMaletic因为有时正在运行的代码已经是调用堆栈中其他地方的动画的一部分.这样可以不对动画进行动画处理. (5认同)

Mav*_*ick 6

斯威夫特 3+

UIView.performWithoutAnimation {
            // Update UI that you don't want to animate
        }
Run Code Online (Sandbox Code Playgroud)