【问题标题】:IOS - Quartzcore custom UIView with shadowIOS - Quartzcore 自定义 UIView 带阴影
【发布时间】:2013-03-27 12:08:16
【问题描述】:

我正在实现一个自定义 UIView 以显示在 UIScrollview 中。问题是我需要视图来投下阴影,所以我这样做了:

#import <QuartzCore/QuartzCore.h>
@implementation CustomView

-(void)setupView{


  self.layer.shadowColor = [UIColor blackColor].CGColor;
  self.layer.shadowOpacity = 0.5;
  self.layer.shadowRadius = 1;
  self.layer.shadowOffset = CGSizeMake(.6f, .6f);
  self.layer.cornerRadius = 2;

  [...]

}

-(id)initWithFrame:(CGRect)frame{
  if((self = [super initWithFrame:frame])){
    [self setupView];
  }

  return self;
}

[...]

关键是,当我构建并运行它时,滚动视图非常慢,我只需要删除那些我破解“self.layer”的行,滚动视图就会再次快速流畅。

向我的自定义视图添加阴影的正确方法是什么?

【问题讨论】:

    标签: ios uiview nib shadow quartz-core


    【解决方案1】:

    这与UIView 在移动时必须进行的所有重绘有关。

    如果您将图层栅格化,它会变得更平滑,这应该可以解决问题:

    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;
    

    您可以尝试添加阴影路径:

    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    

    这可能会有所帮助。

    【讨论】:

    • 这是一种更流畅但仍不流畅的方式。在这之后我能做些什么?
    • 并非如此,这会光栅化图层(将其转换为图像),然后移动并且不再重绘。您在哪种设备上测试您的应用程序?更改了我的答案以包含阴影路径。
    • 好吧,有点奇怪.. iPhone4
    • 在iPhone 4上应该够流畅了,你在滚动的时候有没有做其他的事情?确保滚动视图中没有任何不可见的视图。
    • 我添加了 [UIBezierPath bezierPathWithRect:self.bounds].CGPath 及其流体,但我看不到阴影 =)
    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多