ios 在 iOS4+ 中显式禁用 UIView 动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5443742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:23:36  来源:igfitidea点击:

Explicitly disabling UIView animation in iOS4+

iosanimationexplicit

提问by P i

I have been reading that Apple recommends to use block-based animations instead of CATransaction

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

Before, I was using this code to disable animations:

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

[CATransaction begin];
[CATransaction setDisableActions: YES];
// !!! resize
[CATransaction commit];

Is there a new recommended method to do this, or is this still okay?

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

回答by Joshua Weinberg

[UIView setAnimationsEnabled:NO];
//animate here
[UIView setAnimationsEnabled:YES];

回答by Brentley Jones

For iOS 7 and above this can now be accomplished with:

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

[UIView performWithoutAnimation:^{
    // Changes we don't want animated here
    view.alpha = 0.0;
}];

回答by Maverick

Swift 3+

斯威夫特 3+

UIView.performWithoutAnimation {
            // Update UI that you don't want to animate
        }

回答by Jacob Foshee

For MonoTouch (C#) users, here is a helper class:

对于 MonoTouch (C#) 用户,这里是一个帮助类:

public class UIViewAnimations : IDisposable
{
    public UIViewAnimations(bool enabled)
    {
        _wasEnabled = UIView.AnimationsEnabled;
        UIView.AnimationsEnabled = enabled;
    }

    public void Dispose()
    {
        UIView.AnimationsEnabled = _wasEnabled;
    }

    bool _wasEnabled;
}

Example:

例子:

using (new UIViewAnimations(false))
    imageView.Frame = GetImageFrame();

回答by Michael

// Disable animations
UIView.setAnimationsEnabled(false)

// ...
// VIEW CODE YOU DON'T WANT TO ANIMATE HERE
// ...

// Force view(s) to layout
yourView(s).layoutIfNeeded()

// Enable animations
UIView.setAnimationsEnabled(true)