16

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

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

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

是否有新的推荐方法可以做到这一点,或者这仍然可以吗?

4

5 回答 5

34
[UIView setAnimationsEnabled:NO];
//animate here
[UIView setAnimationsEnabled:YES];
于 2011-03-26T16:50:09.493 回答
25

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

[UIView performWithoutAnimation:^{
    // Changes we don't want animated here
    view.alpha = 0.0;
}];
于 2014-07-03T14:24:14.513 回答
8

斯威夫特 3+

UIView.performWithoutAnimation {
            // Update UI that you don't want to animate
        }
于 2018-02-19T09:45:58.840 回答
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();
于 2012-07-27T22:43:39.170 回答
2
// 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)
于 2018-05-31T00:46:12.527 回答