Explicitly disabling UIView animation in iOS4+

24,800

Solution 1

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

Solution 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;
}];

Solution 3

Swift 3+

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

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

Solution 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)
Share:
24,800
P i
Author by

P i

Teen-coder (Linux/C++) -> math-grad -> tutor -> freelancer (Mobile specializing in Audio/DSP) -> Software Engineer -> DSP Consultant -> CTO (for cueaudio.com) -> Doing my own thing My most recent placement was as lead engineer for cueaudio.com. However my role quickly elevated to CTO and head of technical staffing. I was able to set the company on the right track by creatively sourcing key talent. The company recovered its $3M seed funding within the first 30 months of operation.

Updated on July 14, 2022

Comments

  • P i
    P i almost 2 years

    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?

  • jasongregori
    jasongregori almost 13 years
    The problem with using the UIView approach is that you cannot nest it like you can with CATransaction approach. So if you had a block of code that disabled animations and made a call to something else that disabled animations inside that block the animations would be reenabled before the end of your block.
  • Joshua Weinberg
    Joshua Weinberg almost 12 years
    easy way to fix that is to check what the state was, using areAnimationsEnabled and then restore it to that after you're done
  • Jacob Foshee
    Jacob Foshee about 11 years
    Glad someone else found it useful. In gist form: gist.github.com/jfoshee/5008117
  • BergP
    BergP about 11 years
    What can i do, if i need to disable animation for concrete view object? Not for all Views
  • Prasad Devadiga
    Prasad Devadiga almost 11 years
    @JoshuaWeinberg I am facing an issue when using this, could you please check this stackoverflow.com/questions/16806816/…
  • Greg Maletic
    Greg Maletic over 9 years
    I still don't get the use-case for this. Why would I bother wrapping this in a block? Why not just say "view.alpha = 0.0;" and be done with it?
  • Brentley Jones
    Brentley Jones almost 9 years
    @GregMaletic Because sometimes the code being run is already part of an animation somewhere else in the call stack. This allows the update to not be animated.
  • Greg Maletic
    Greg Maletic almost 9 years
    Ah…so the idea is that this is wrapped by some other [UIView animate] call, and this is a little internal set-aside, so I can include things in that animate block that I don't want animated?
  • Brentley Jones
    Brentley Jones almost 9 years
    Correct. Sometimes you also get caught up in an animation by the system.