在 iPhone 和 iPhone 上使用 Quartz 绘制阴影速度很慢iPad。另一种方式?
当我发现在 iPhone/iPad 上向 UIView 添加阴影是多么容易时,我感到非常兴奋。
只需在 Xcode 中添加框架,将导入添加到文件顶部:
#import <QuartzCore/QuartzCore.h>
然后稍后:
self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;
虽然这确实在我的应用程序中创建了美丽的阴影,但现在显示视图时它也会滞后到死亡......即使在外部启动时也是如此调试器的。有什么我忘记了或者这种方法对于更大的视图来说不实用吗?
作为参考,我在此处发布了屏幕截图。
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 of the file:
#import <QuartzCore/QuartzCore.h>
Then later:
self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;
While this does create a beautiful shadow in my app, it also lags it to death now when the view is shown... even when launched outside of the debugger. Is there something I'm forgetting or is this method just not practical for larger views?
For reference, I posted a screenshot here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该设置
shadowPath属性
。这就是 CoreGraphics 能够优化阴影的方式。
例如,如果您的视图是一个不透明的矩形:
You should set the
shadowPath
property. It is how CoreGraphics is able to optimize shadows.For example, if your view is an opaque rectangle:
我想我应该回答一下,因为我不想让这颗宝石被埋在评论中。
除了上面cobbal的回答很有帮助之外,oculus还提到了以下针对非矩形阴影的优化,例如文本阴影。
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.