Explicitly disabling UIView animation in iOS4+

前端 未结 5 951
予麋鹿
予麋鹿 2020-12-31 07:33

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

Before, I was using this code to disable animations:



        
相关标签:
5条回答
  • 2020-12-31 08:06

    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();
    
    0 讨论(0)
  • 2020-12-31 08:09
    [UIView setAnimationsEnabled:NO];
    //animate here
    [UIView setAnimationsEnabled:YES];
    
    0 讨论(0)
  • 2020-12-31 08:10

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

    [UIView performWithoutAnimation:^{
        // Changes we don't want animated here
        view.alpha = 0.0;
    }];
    
    0 讨论(0)
  • 2020-12-31 08:26

    Swift 3+

    UIView.performWithoutAnimation {
                // Update UI that you don't want to animate
            }
    
    0 讨论(0)
  • 2020-12-31 08:30
    // 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)
    
    0 讨论(0)
提交回复
热议问题