2014-12-05 89 views
1

我身边有15次为我申请QuartzCore阴影这样的:阴影滞后于用户界面

for button in buttonsArray { 
    button.layer.shadowOpacity = 0.75 
    button.layer.shadowColor = UIColor.blackColor().CGColor 
    button.layer.shadowRadius = 2.0 
    button.layer.shadowOffset = CGSize(width: 0.4, height: 1.2) 
} 

当我(在这个例子中一样)1个视图有很多这样的阴影真的开始缓慢和滞后的用户界面。
我该如何解决它,或者我有什么其他的替代方案?谢谢!

+0

在哪个设备上? – holex 2014-12-05 08:50:10

+0

@holex不在模拟器上,我只在iOS 8 iPhone 5上测试过它,但我认为它落后于所有设备 – Eilon 2014-12-05 08:53:32

+0

可能的重复[为什么为任何图层添加阴影太重?使用QuartzCore](http://stackoverflow.com/questions/16197121/why-add-shadow-to-any-layer-is-too-heavy-using-quartzcore) – 2014-12-05 10:02:38

回答

1

查看WWDC 2014视频“适用于iOS应用程序的高级图形和动画”。

https://developer.apple.com/videos/wwdc/2014/

35:50在...它谈到为什么使用这种类型的代码来生成阴影造成额外关闭屏幕通行证的GPU,因为它计算出阴影的形状。

建议的解决方案是同时使用shadowPath属性层上,如果你已经知道了影子的形状......

这是一个伟大的视频观看。

+0

'shadowPath'是** **的方式走。 – Cyrille 2014-12-05 10:14:23

0

根据按钮的外观,一个想法是使用包含阴影的按钮的背景图像,而不是在代码中添加阴影。您还可以使用resizableImageWithCapInsets来制作各种可能的按钮尺寸。在resizableImageWithCapInsets

UIImage* image = [UIImage imageNamed:@"button.png"]; 
UIEdgeInsets insets = UIEdgeInsetsMake(16, 6, 16, 6); // change the insets to fit your needs 
image = [image resizableImageWithCapInsets:insets]; 
[button setBackgroundImage:image forState:UIControlStateNormal]; 

文档可在

找到这个解决方案可能不适合你,取决于你如何按钮看起来。但是使用图片而不是从代码添加阴影应该会加速您的代码。