17

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?

P i
  • 29,020
  • 36
  • 159
  • 267

5 Answers5

37
[UIView setAnimationsEnabled:NO];
//animate here
[UIView setAnimationsEnabled:YES];
Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
  • 2
    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. – jasongregori May 04 '11 at 17:57
  • easy way to fix that is to check what the state was, using `areAnimationsEnabled` and then restore it to that after you're done – Joshua Weinberg May 20 '12 at 05:05
  • What can i do, if i need to disable animation for concrete view object? Not for all Views – BergP Apr 19 '13 at 12:41
  • @JoshuaWeinberg I am facing an issue when using this, could you please check this http://stackoverflow.com/questions/16806816/orientation-without-animation-giving-weird-result-when-uialertview-is-present – Prasad Devadiga Jun 03 '13 at 05:18
27

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

[UIView performWithoutAnimation:^{
    // Changes we don't want animated here
    view.alpha = 0.0;
}];
Brentley Jones
  • 417
  • 4
  • 8
  • 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? – Greg Maletic Sep 26 '14 at 17:30
  • 5
    @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. – Brentley Jones Jul 10 '15 at 12:50
  • 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? – Greg Maletic Jul 10 '15 at 16:48
  • Correct. Sometimes you also get caught up in an animation by the system. – Brentley Jones Jul 10 '15 at 20:41
  • Yep, I just found this and used it for this exact use-case: Getting caught up in a system animation, or perhaps an animation somewhere in legacy code that I couldn't find! – simonthumper Aug 17 '23 at 08:15
10

Swift 3+

UIView.performWithoutAnimation {
            // Update UI that you don't want to animate
        }
Maverick
  • 3,209
  • 1
  • 34
  • 40
3

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();
Jacob Foshee
  • 2,704
  • 2
  • 29
  • 48
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)
Michael
  • 9,639
  • 3
  • 64
  • 69