开发者

Mimic CALayer shadow properties found in iPhone OS 3.2 for OS 3.1

开发者 https://www.devze.com 2022-12-30 08:17 出处:网络
The CALayer shadow properties like shadowOffset, shadowRadius, shadowColor are not available in iPhone OS versions below 3.2 and I\'m wondering how I could mimic that functionality for use with 3.1 an

The CALayer shadow properties like shadowOffset, shadowRadius, shadowColor are not available in iPhone OS versions below 3.2 and I'm wondering how I could mimic that functionality for use with 3.1 and below.

I want to use this to be able to add drop shadows to UIViews in a clean way so that the shadows are drawn at layer level somehow, and not by drawing it in a view's -(void)drawRect:(CGRect)rect method which requires to sh开发者_如何学JAVArink the actual views frame to accomodate for the shadow. (This shrinking approach have been proposed in the other UIView drop shadow related questions I found here on SO).

I was thinking a layered approach would be cleaner. For example I tried creating subclassing CALayer to which I added a separate shadow layer as a sublayer, but then that would be drawn on top of whatever was draw in the drawRect: method of the UIView that had the main layer as backing layer.

I've also tried implementing the subclass CALayer's drawInContext: something like this,

- (void)drawInContext:(CGContextRef)ctx {
// code to draw shadow for a frame the size of the layer's frame        
[super drawInContext:ctx];
}

But then the shadow is still clipped to the current clipping bounding box of the context, which seems to be the layers own frame.

I also had some idea of redirecting the drawing of the main layer to a sublayer, which would be placed above another sublayer which had the shadow drawn onto it. Then I would probably get rid of the clipping and the shadow would be farthest away. But I couldn't really wrap my head around how I would do that, and it doesn't really feel like a clean approach.

Any ideas on how to go about this? Just to make clear how my UIView drop shadow related question is different from the other ones I found here on SO; I do not want to shrink the actual drawing frame of a UIView to accomodate for a shadow. I want it to somehow be on a separate layer in the background, whithout beeing clipped.


You could create an image simulating a shadow and just scale that, probably automatically by giving it a pointer to any view. It would then automatically adjust itself and position below that in whatever superview that view has.

This works pretty nicely. Shadow should be about 50% of screen size, so the average expansion / compression is pretty invisible. If it has a big radius you really can't tell if it's scaled or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消