I have a semi-transparent view with a drop shadow. I mocked it up in photoshop, which doesn't draw the shadow under the object. Then I made it in xcode, but it obviously does draw the shadow underneath. I was wondering if there's a way to avoid this. In other words - have the shadow only appear outside of the shape?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You probably want to set the rasterized property of the layer to YES, which will composite the shadow before other effects such as opacity are applied to the view.
Note however that this has a big performance impact. There's a handy table of different shadow implementations and their performance implications here: http://www.omnigroup.com/blog/entry/ipad_drop_shadow_performance_test/
回答2:
Use CGContextEOClip
to set up a clipping path that excludes your shape, then set up your shadow and draw the shape with full opacity. There is some example code in this answer.