IOS我在ViewController中创建自定义视图。当它滚动时,速度非常慢

IOS我在ViewController中创建自定义视图。当它滚动时,速度非常慢,ios,uiview,draw,layer,Ios,Uiview,Draw,Layer,我在控制器上放置了一个自定义视图,自定义视图的高度非常高。 然后当我滚动控制器时,速度非常慢 测试环境:IOS5.0+,ipod touch 自定义视图: @interface UICommonView : UIView //..... @end @implementation UICommonView //..... -(void)setBorder:(CGFloat)width color:(UIColor*)color { self.frameColor =

我在控制器上放置了一个自定义视图,自定义视图的高度非常高。 然后当我滚动控制器时,速度非常慢

测试环境:IOS5.0+,ipod touch

自定义视图:

 @interface UICommonView : UIView
    //.....
 @end

 @implementation UICommonView
 //.....
 -(void)setBorder:(CGFloat)width color:(UIColor*)color
 {
     self.frameColor = color;
     self.frameWidth = width;
     [self.layer setBorderWidth:width];  
     [self.layer setBorderColor:[color CGColor]];  
 }
 -(void)makeShadow
 {
     self.layer.shadowOpacity = 0.7f;
     self.layer.shadowOffset = CGSizeMake(5.0f,3.0f);
     self.layer.shadowColor =[[UIColor blackColor] CGColor];
 }
 -(void)makeCornerRadius:(CGFloat)_cornerRadius
{
    self.cornerRadius = _cornerRadius;
    [self.layer setMasksToBounds:NO];
    [self.layer setCornerRadius:_cornerRadius];
 }
 @end
控制器:

//contentView is UICommonView  
//scrollView is UIScrollView on controller

-(void)viewDidLoad
{
     [self.contentView makeShadow];
     [self.contentView makeCornerRadius:5.0f];
     [self.contentView setBorder:4.0f color:[UIColor white]];
     // self.contentView.backgroundColor = //......
     [self.contentView setFrame:CGRectMake(0,0,320,1200)];
     [self.scrollView addSubview:contentView];
     scrollerView.contentSize = CGSizeMake(320,1300);
     //add other view
     //I tested, only custom View impact speed
}

如何优化它。谢谢

如果给阴影提供一条路径,阴影通常会更有效:

-(void)makeShadow
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds cornerRadius:self.cornerRadius];
    self.layer.shadowPath = path.CGPath; 
    self.layer.shadowOpacity = 0.7f;
    self.layer.shadowOffset = CGSizeMake(5.0f,3.0f);
    self.layer.shadowColor =[[UIColor blackColor] CGColor];
}
但是,因为您同时引用了视图的rect和拐角半径,所以应该在这两个视图中调用
[self-makeShadow]

-(void)makeCornerRadius:(CGFloat)\u cornerRadius

以及内部

-(void)setFrame
这样,无论何时更改帧或角半径,阴影都会相应地更新。这将使您的性能更平稳