在 iPhone OS 3.2 中发现 OS 3.1 的模仿 CALayer 阴影属性
ShadowOffset、shadowRadius、shadowColor 等 CALayer 阴影属性在 3.2 以下的 iPhone OS 版本中不可用,我想知道如何模仿该功能以与 3.1 及以下版本一起使用。
我想用它来以一种干净的方式向 UIView 添加阴影,以便以某种方式在图层级别绘制阴影,而不是通过在视图的 -(void)drawRect:(CGRect)rect 中绘制它
方法需要缩小实际视图框架以适应阴影。 (这种缩小方法已在我在此处找到的其他 UIView 阴影相关问题中提出)。
我认为分层方法会更干净。例如,我尝试创建子类 CALayer,向其中添加一个单独的阴影层作为子层,但随后它将绘制在以主层作为支持层的 UIView 的 drawRect: 方法中绘制的内容之上。
我也尝试过实现 CALayer 的子类 drawInContext:类似这样,
- (void)drawInContext:(CGContextRef)ctx {
// code to draw shadow for a frame the size of the layer's frame
[super drawInContext:ctx];
}
但是阴影仍然被剪切到上下文的当前剪切边界框,这似乎是图层自己的框架。
我还有一些想法,将主图层的绘制重定向到子图层,该子图层将放置在另一个绘制有阴影的子图层之上。然后我可能会去掉剪裁,阴影就会离得最远。但我无法真正理解如何做到这一点,而且这感觉并不是一个干净的方法。
关于如何解决这个问题有什么想法吗?只是为了弄清楚我的 UIView 投影相关问题与我在这里找到的其他问题有何不同;我不想缩小 UIView 的实际绘图框架以适应阴影。我希望它以某种方式位于背景中的单独图层上,而不被剪裁。
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 shrink 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个模拟阴影的图像,然后对其进行缩放,可能会通过为其提供指向任何视图的指针来自动缩放。然后,它会自动调整自身并定位在该视图具有的任何超级视图中的下方。
这效果非常好。阴影应占屏幕尺寸的 50% 左右,因此平均扩展/压缩是相当不可见的。如果它的半径很大,你真的无法判断它是否被缩放。
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.