我一直在阅读 Apple 建议使用基于块的动画而不是 CATransaction
之前,我使用此代码禁用动画:
[CATransaction begin];
[CATransaction setDisableActions: YES];
// !!! resize
[CATransaction commit];
是否有新的推荐方法可以做到这一点,或者这仍然可以吗?
[UIView setAnimationsEnabled:NO];
//animate here
[UIView setAnimationsEnabled:YES];
对于 iOS 7 及更高版本,现在可以通过以下方式完成:
[UIView performWithoutAnimation:^{
// Changes we don't want animated here
view.alpha = 0.0;
}];
斯威夫特 3+
UIView.performWithoutAnimation {
// Update UI that you don't want to animate
}
对于 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();
// 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)