我有一个 UIScrollView
,其功能与 Facebook 新闻源 非常相似。我认为我的元素正在减慢滚动帧率。通过排除过程,我发现阴影减慢了我的应用程序的速度。
ScrollView 内的UIView
框具有这样的配置:
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0, 2)
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 0.15
就像新闻提要一样,我的 ScrollView 有很多框,因此 UIView
具有自己的阴影。如何在不减慢我的应用程序速度的情况下进行此操作?
最佳答案
有很多关于加速 UIScrollViews 的内容:
- CALayer - Shadow causes a performance hit?
- https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/
如果您使用自定义 CALayer 实例——尤其是阴影——需要处理能力的东西,您应该使用
scrollView.layer.shouldRasterize = YES;
scrollView.layer.rasterizationScale = [UIScreen mainScreen].scale;
此外,shadowPath
也可以加速您的 ScrollView ,如下所示:
[scrollView.layer setShadowPath:[[UIBezierPath bezierPathWithRect:myView.bounds] CGPath]];
设置shadowPath可以让iOS在每次绘制 View 时不必重新计算应该如何绘制阴影,从而提高性能。
快速地,像这样:
let shadowPath = UIBezierPath(rect: view.bounds);
view.layer.masksToBounds = false;
view.layer.shadowColor = UIColor.blackColor().CGColor;
view.layer.shadowOffset = CGSizeMake(0, 0.5);
view.layer.shadowOpacity = 0.2;
view.layer.shadowPath = shadowPath.CGPath;
关于ios - 阴影会减慢 UIScrollView 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601695/