Fastest way to do shadows on iOS?

前端 未结 2 841
执笔经年
执笔经年 2020-12-07 08:27

QuartzCore .layer.shadow\'s suck up performance. They appear to need to be re-rendered every time something changes, causing everything to lag.

Coregraphics gradient

2条回答
  •  抹茶落季
    2020-12-07 08:47

    Adding a shadowPath should give you a huge performance boost. The following example assumes you only want the shadow on the sides of your view

    CGPathRef path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    [view.layer setShadowPath:path];
    

    EDIT: On default a CALayer draws a shadow during animations, the following code allows you to cache the shadow as a bitmap and reuse it instead of redrawing it:

    self.view.layer.shouldRasterize = YES;
    // Don't forget the rasterization scale
    // I spent days trying to figure out why retina display assets weren't working as expected
    self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;
    

提交回复
热议问题