0

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

测试环境: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
}

如何优化它。谢谢!!

4

1 回答 1

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

这样,无论何时更改框架或角半径,阴影都会相应更新。这应该会给你更流畅的性能。

于 2012-11-10T06:02:23.077 回答