Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ppilone

macrumors 6502
Original poster
Jan 20, 2008
361
0
Hey,

I've got a UIView with a slight drop shadow using its layer's shadow properties. I set up the shadow as such:

Code:
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.clipsToBounds = NO;
    self.layer.masksToBounds = NO;
    
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.f, 0.f);
    self.layer.shadowOpacity = 0.6f;
    self.layer.shadowRadius = 10.f;

...

}

I then make sure to set the shadowPath on the layer in order to improve performance.

Code:
- (void)layoutSubviews
{
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

...
}

I currently do this in the view's layoutSubviews method in order to catch an resizing I do after it's loaded from the nib. I adjust the position and size of this view in response to some user interaction and on rotation.

The issue I'm having is that by defining the shadowPath the shadow either doesn't change (if I remove setting the shadowPath in layoutSubviews) or it does not animate with the view's animation and simply "jumps" to the target size and location of the view.

I know that shadowPath supports explicit animations, but I'm not sure the right place to insert that animation. I've also tried creating a custom CALayer and use that as the view's layer class. Since I'm defining the shadowPath that doesn't solve the issue.

Any suggests on how to animate the position/size of a view's shadowPath?
 
I would just avoid this problem by drawing the shadow directly.

Create a small bitmap graphics context. Draw a small shadow that can be used as a stretchable UIImage. Set that image in a UIImageView and set the autoresizing appropriately.

For various reasons involving the details of Core Animation compositing, this will also give you better performance than setting the shadow through the CALayer.

Is there a reason you would absolutely have to stick with the CALayer?
 
I would just avoid this problem by drawing the shadow directly.

Create a small bitmap graphics context. Draw a small shadow that can be used as a stretchable UIImage. Set that image in a UIImageView and set the autoresizing appropriately.

For various reasons involving the details of Core Animation compositing, this will also give you better performance than setting the shadow through the CALayer.

Is there a reason you would absolutely have to stick with the CALayer?

No reason other than when working properly CALayer produces really nice looking shadows.

Would you mind sharing the snippet of code that draws the shadow? I'm familiar with drawing to a bitmap graphics context but interested in the values and approach to drawing a proper shadow.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.