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?

3
  • This question is similar, but not quite what I'm looking for: stackoverflow.com/questions/4730110/… Dec 19, 2011 at 20:54
  • What do you need to be different from that other question?
    – rob mayoff
    Dec 19, 2011 at 21:23
  • That question only seems to answer fading in a view, not updating a view with a fade transition. Dec 19, 2011 at 22:43

2 Answers 2

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];
5
  • 1
    You are the man! Worked perfectly. I really should brush up on my Core Animation, shouldn't I? Dec 20, 2011 at 4:49
  • Ha ha, maybe. I learned a lot from the 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, 2011 at 5: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! :) Dec 20, 2011 at 5:19
  • You should not put animation code in the drawRect: method Oct 2, 2012 at 0:07
  • @titaniumdecoy You are right, but I didn't know that when I originally answered. :) I have changed my answer.
    – rob mayoff
    Oct 2, 2012 at 4: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
                }];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.