Explicitly disabling UIView animation in iOS4+

随声附和 提交于 2019-11-29 17:35:02

问题


I have been reading that Apple recommends to use block-based animations instead of 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?


回答1:


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



回答2:


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

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



回答3:


Swift 3+

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



回答4:


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

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();



回答5:


// 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)


来源:https://stackoverflow.com/questions/5443742/explicitly-disabling-uiview-animation-in-ios4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!