iOS应用性能调优的25个建议和技巧(19)

    xiaoxiao2021-11-30  24

    19. 设定ShadowPath

    如何在一个View或者一个layer上加一个shadow呢,QuartzCore框架是很多开发者的选择:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    #import <QuartzCore/QuartzCore.h>

     

    // Somewhere later ...

    UIView *view = [[UIView alloc] init];

     

    // Setup the shadow ...

    view.layer.shadowOffset = CGSizeMake(-1.0f, 1.0f);

    view.layer.shadowRadius = 5.0f;

    view.layer.shadowOpacity = 0.6;

     

    看起来很简单,对吧。

    可是,坏消息是使用这个方法也有它的问题 Core Animation不得不先在后台得出你的图形并加好阴影然后才渲染,这开销是很大的。

    使用shadowPath的话就避免了这个问题: view.layer.shadowPath = [[UIBezierPath bezierPathWithRect:view.bounds] CGPath];

    使用shadow path的话iOS就不必每次都计算如何渲染,它使用一个预先计算好的路径。但问题是自己计算path的话可能在某些View中比较困难,且每当viewframe变化的时候你都需要去update shadow path.

    想了解更多可以看看Mark Pospesel这篇

    转载请注明原文地址: https://ju.6miu.com/read-679122.html

    最新回复(0)