iOSのCALayerで影を付けるときはShadowPathを指定すべき

UIViewのlayerプロパティで影を付ける時などはshadowpathを設定する。

shadowpathを指定しない場合はUIViewの形状を計算して、影をつけるので計算時間がかかる。

shadowpathを指定することで、影の形状を指定し処理を高速化できる。

CollectionViewやTableViewで影付きのCustomCellを使っている場合は特に有用である。

例えば以下の様にする。

cell.layer.masksToBounds = NO;
cell.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
cell.layer.shadowOpacity = 0.9f;
cell.layer.shadowRadius = 2.0f;
cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.layer.bounds].CGPath;

UIBezierPathでの影の形状の指定は以下のサイトが参考になる。

Fun shadow effects using custom CALayer shadowPaths | iOS/Web Developer's Life in Beta