在iOS4 +中明确禁用UIView动画

时间:2011-03-26 16:47:37

标签: ios animation explicit

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

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

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

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

5 个答案:

答案 0 :(得分:30)

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

答案 1 :(得分:25)

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

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

答案 2 :(得分:4)

Swift 3 +

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

答案 3 :(得分:3)

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

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

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

    bool _wasEnabled;
}

示例:

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

答案 4 :(得分:0)

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