IOS我在ViewController中制作自定义视图。滚动时非常慢

时间:2012-11-10 05:19:26

标签: ios uiview draw layer

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

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

customView:

 @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
}

如何优化它。谢谢!

1 个答案:

答案 0 :(得分:0)

如果你给他们一条路径,那么阴影效率通常更高:

-(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];
}

但是,因为您正在引用视图的矩形和圆角半径,所以应该在

中调用[self makeShadow]

-(void)makeCornerRadius:(CGFloat)_cornerRadius

以及

-(void)setFrame

这样,每当帧或角半径改变时,阴影将相应地更新。这应该会给你更顺畅的表现。