1

我正在实现一个自定义 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”的行,滚动视图再次快速流畅。

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

4

1 回答 1

2

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

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

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

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

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

这可能会有所帮助。

于 2013-03-27T12:30:57.420 回答