CALayer - Shadow causes a performance hit?

后端 未结 5 1932
Happy的楠姐
Happy的楠姐 2020-12-13 02:33

So I am doing some custom animations on my navigationcontroller and the way it pushes and pops the viewControllers.

Everything runs smooth. As soon as I add the foll

相关标签:
5条回答
  • 2020-12-13 02:44

    Using shadowPath instead of shadowOffset.

    theView.layer.shadowPath = [UIBezierPath bezierPathWithRect:theView.bounds].CGPath;
    

    Check this post: iphone - Animation's performance is very poor when view's shadow is on

    0 讨论(0)
  • 2020-12-13 02:45

    You should expect a slowdown from adding a shadow. A shadowRadius of 20 is very high and will be especially slow.

    The other key to improve shadow rendering speed: set the shadowPath property. It can help dramatically.

    0 讨论(0)
  • 2020-12-13 03:00

    Swift 5.3. add this code.

    myView -> UIView, collectionViewCell or tableViewCell can be.

    myview.layer.shadowPath = UIBezierPath(rect: cell.bounds).cgPath
    myview.layer.shouldRasterize = true
    myview.layer.rasterizationScale = UIScreen.main.scale
    
    0 讨论(0)
  • 2020-12-13 03:02

    Yes, shadow's are very expensive (especially a shadow that big -- play with the radius and you'll notice it makes a huge difference in the degree of slowdown you experience). One way to improve performance is to render it once to a CGImageContext and just display that image instead of having the layer re-render the shadow every time it redraws (but this doesn't work if the shadow needs to animate or something).

    0 讨论(0)
  • 2020-12-13 03:09
    self.view.layer.shouldRasterize = YES;
    self.view.layer.rasterizationScale = UIScreen.mainScreen.scale;
    

    I was recently having some issues with slow CALayer shadows, and that simple line of code fixed up everything for me!

    0 讨论(0)
提交回复
热议问题