Draw a shadow which is hidden under the semi-trans

2020-07-24 04:49发布

问题:

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.