UIView animation (backgroundColor) + drawRect:
UIView animation (backgroundColor) + drawRect:
- Subject: UIView animation (backgroundColor) + drawRect:
- From: Roland King <email@hidden>
- Date: Wed, 02 Mar 2011 21:12:33 +0800
I was trying to animate the backgroundColor of one of my UIView subclasses yesterday using
[ UIView animateWithDuration:2.0 animations:^{ [ myView setBackgroundColor:someColor ]; } ];
but no matter what the duration set, the background color was changing instantly. Whilst looking to see if there were two places I called setBackgroundColor: (there weren't) I realized that my UIView subclass has its own drawRect: method (this view is basically a piece of paper with some lines drawn on it, so it needs a custom drawRect: method). Commenting that out, the view background animated properly, but of course I had no content. Even just adding an empty drawRect: method was enough to defeat the animation.
My assumption is that if there is no custom drawing, the view can smoothly animate its layer between the two colors, and animate any other simple properties, but if there is custom drawing, it doesn't want to call that constantly as it animates the color change, so animation doesn't happen.
Is there anything I missed which will make this work,? I was thinking that the other transition animation methods ( [ UIView transitionWithView: .. ] ) take a 'before' and 'after' snapshot and then, if you are flipping, animate one away, and one in. I perhaps expected that UIView to do basically that, and crossfade the old view out and the new one in, but I see no option for that, just flips and curls, I want fade. This would be more generally useful than just this example.
Obviously I have a few other options including building an entirely new UIView on a color change, adding them both and fading their alphas in and out, or splitting my 'paper' into a background view and an otherwise-clear lines view. That may well be the right way to go with this, however I was interested to know if there was some part of UIView animation which would automatically handle a smooth transition of one single UIView between two drawn states. _______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden