19

In TwUI, there is a method called redraw on TUIView. It forces the view to redraw, but it also comes with a free fading animation between the old and new state of the view.

I'm wondering if something like that is possible in a normal UIView. Basically, how can I redraw the view (setNeedsDisplay) with a fading animation between the old and the new states?

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161

2 Answers2

46

Use +[UIView transitionWithView:duration:options:animations:completion:] with the UIViewAnimationOptionTransitionCrossDissolve option, and inside the animation block, force the view's layer to redraw its contents immediately.

[myView setNeedsDisplay];
[UIView transitionWithView:myView duration:1
    options:UIViewAnimationOptionTransitionCrossDissolve
    animations:^{
        [myView.layer displayIfNeeded];
    } completion:nil];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    You are the man! Worked perfectly. I really should brush up on my Core Animation, shouldn't I? – sudo rm -rf Dec 20 '11 at 04:49
  • Ha ha, maybe. I learned a *lot* from the *[Core Animation Essentials](https://developer.apple.com/videos/wwdc/2011/#core-animation-essentials)* video from WWDC 2011. But for your question I also had to write a little test program that changed the contents property of a code-created CALayer and see what animation it attached. – rob mayoff Dec 20 '11 at 05:10
  • Thanks for that link, I'll watch it sometime. I also bought the book *Core Animation* by Zarra & Long, but I'm ashamed to say I haven't read it yet. Time to take a reading break! :) – sudo rm -rf Dec 20 '11 at 05:19
  • You should **not** put animation code in the `drawRect:` method – titaniumdecoy Oct 02 '12 at 00:07
  • @titaniumdecoy You are right, but I didn't know that when I originally answered. :) I have changed my answer. – rob mayoff Oct 02 '12 at 04:26
1

How about using a cross-dissolve UIView transition?

[UIView transitionWithView:aView 
                  duration:TIME_INTERVAL 
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{
                    // Change the view's state
                } 
                completion:^(BOOL finished) {
                    // Completion block
                }];
sho
  • 759
  • 5
  • 16