Drawing shadow with Quartz is slow on iPhone & iPad. Another way?

前端 未结 2 1716
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 17:45

I was pretty excited when I found out just how easy it is to add shadows to my UIViews on the iPhone/iPad.

Just add the framework in Xcode, add the import to the top

相关标签:
2条回答
  • 2020-12-23 18:27

    You should set the shadowPath property. It is how CoreGraphics is able to optimize shadows.

    For example, if your view is an opaque rectangle:

    self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.contentView.bounds].CGPath;
    
    0 讨论(0)
  • 2020-12-23 18:41

    Thought I should make an answer because I didn't want this gem to get buried in the comments.

    In addition to cobbal's answer above, which helped a lot, occulus also mentioned the following optimization for non-rectangular shadows, such as text shadows.

    self.contentView.layer.shouldRasterize = YES;
    // Fix visual degradation when rasterizing on high-density screens:
    self.contentView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    0 讨论(0)
提交回复
热议问题