Fun shadow effects using custom CALayer shadowPaths

本文介绍如何通过设置自定义CGPath来改善CALayer阴影性能,并探讨了使用不同形状路径(如梯形、椭圆形及曲线)创建独特阴影效果的方法。

I recently had to improve the performance of a few views that utilized CALayer-based shadows on rounded-rect UIView objects. On this particular iPad application, when the device was rotated, the views rotated quite a lot slower than we would have hoped. It wasn’t a show-stopper, but the jerky rotation animation made it look cheap and unpolished. The easiest way to have our cake, and eat it too, was to set a custom CGPath to the layer’s shadowPath property. This told UIKit to set the inside of the path to opaque, reducing the amount of work the rendering engine needed to perform.

The resulting image, as you can see above, has a shadow as you’d expect. But since we’ve declared the shape the path will have, the iPad can drastically improve its rendering performance.

Through that process however, I decided to see what sort of effects I could pull off by passing in a path otherthan the default rectangular bounds of the layer. Since you can create any sort of path you want, I considered the different effects I could get away with by making non-rectangular paths and using them as shadows.

Trapezoidal CGPath

Trapezoidal shadow providing the illusion of depth

By carefully drawing a trapezoidal shape below and slightly beneath the view, you can give the illusion of depth.

Elliptical CGPath

Elliptical shadows create the illusion of a top-down light source

Just like the trapezoid, there are other effects you can achieve by playing with simple shapes for the use of creating shadows.

Paper-curl effect

Paper curl example using a curved path

By using a control point on a bezier curve, you can make the bottom side of the shadow curve inward, making it appear like the view is printed on paper that has been curled inward.

More possibilities than can be covered

There are plenty of other possibilities, more than can be covered here. Creating CGPathRef objects, either using UIBezierCurve or by using Quartz2D drawing methods, can easily step through composing shadows. Use a CGAffineTransform object to manipulate your path to stretch, scale, or rotate it as needed. Once you realize what your possibilities are, you can add an extra degree of polish to your application with very little effort.

If you want to play with the source use to create these examples, make sure you download the Xcode project ShadowTest.

Please post comments of any fun variations you manage to come up with!

Note: Special thanks go out to my beautiful wife Deanna for the picture of her Blood Elf Mage cosplay outfitshe put together for Halloween.

From: http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值