• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: UIView animation (backgroundColor) + drawRect:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UIView animation (backgroundColor) + drawRect:


  • Subject: Re: UIView animation (backgroundColor) + drawRect:
  • From: David Duncan <email@hidden>
  • Date: Wed, 2 Mar 2011 13:10:22 -0800

On Mar 2, 2011, at 5:12 AM, Roland King wrote:

> 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.


When you implement -drawRect:, the background color of your view is then drawn into the associated CALayer, rather than just being set on the CALayer as a style property. Now Core Animation can animate both transitions, but UIView disables the animation of a layer's contents, which thus prevents you from getting a contents crossfade (as in the vast majority of cases this wouldn't be what you want).

The crossfade may be exactly what you want in this case. Likely the best way to defeat UIView's override in this case may be to create a subclass of CALayer that overrides -actionForKey:. In that override, you would check for the @"contents" key and return a CABasicAnimation ([CABasicAnimation animation] should do it) and in all other cases return what the default implementation does. This has the downside that you will *always* cross fade and you won't be able to integrate with UIKit's animation APIs (you just get animation for contents on all the time).

Another solution would be to use a super view that contains just the background color and have your lines composited over it (assuming they are simple lines this could be done with simple subviews that are sized for each line). This would allow you to avoid -drawRect: entirely, which would allow you to animate the background color and may offer some memory usage benefits (if you can use subviews).
--
David Duncan

_______________________________________________

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

  • Follow-Ups:
    • Re: UIView animation (backgroundColor) + drawRect:
      • From: Roland King <email@hidden>
References: 
 >UIView animation (backgroundColor) + drawRect: (From: Roland King <email@hidden>)

  • Prev by Date: Customizing the drop highlight on a NSTableView
  • Next by Date: Re: Editing a list of NSMutableStrings
  • Previous by thread: Re: UIView animation (backgroundColor) + drawRect:
  • Next by thread: Re: UIView animation (backgroundColor) + drawRect:
  • Index(es):
    • Date
    • Thread