ios设置RoundCorner在覆盖drawRect后不起作用,但正常的UIView是可以的

时间:2017-11-28 14:17:14

标签: ios objective-c uiview calayer drawrect

我写了一个关于Wave Animation的例子。动画还可以,但我不明白为什么自定义UIView需要添加“ self.layer.masksToBounds = YES ”才能获得圆角

  1. 这是一个自定义的UIView。我重写了它的drawRect。如果我没有将“ masksToBounds ”设置为YES,则圆角消失。

    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.frame = CGRectMake(10, 40, 300, 300);
            self.layer.cornerRadius = 150;
            self.backgroundColor = [UIColor greenColor];
            self.layer.borderColor = [UIColor blueColor].CGColor;
            self.layer.borderWidth = 2;
            //        self.layer.masksToBounds = YES;  //if write this line,the round corner appear
            self.x_move = 0;
            self.y_move = 300;
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGMutablePathRef path = CGPathCreateMutable();
    
        CGContextSetLineWidth(context, 1);
        CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
        CGPathMoveToPoint(path, NULL, 0, 0);
    
        for(float i=0; i<=300; i++){
            float x=i;
            float y = 5 * sin( 0.05 * x+ self.x_move) + self.y_move;
            CGPathAddLineToPoint(path, nil, x, y);
        }
    
        CGPathAddLineToPoint(path, nil, 300, 0);
        CGPathAddLineToPoint(path, nil, 0, 0);
        CGContextAddPath(context, path);
        CGContextFillPath(context);
        CGContextDrawPath(context, kCGPathStroke);
        CGPathRelease(path);
    }
    
    - (void)startAnimation {
        if (self.waveTimer == nil) {
            self.waveTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
        }
    }
    
    - (void)refresh {
        self.x_move += 0.3;
        self.y_move -= 0.2;
        if(self.y_move - 100 < 0.00001){
            [self.waveTimer invalidate];
        }else{
            [self setNeedsDisplay];
        }
    }
    
  2. 的ViewController:

    self.wave = [[waveAnimation alloc] init];
    [self.wave startAnimation];
    [self.view addSubview:self.wave];
    
    1. 这是一个普通的UIView。其“ masksToBunds ”为NO,但其圆角显示正常。与上面的例子相比,为什么要添加,一个不需要。

      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 100, 100)];
      view.backgroundColor = [UIColor greenColor];
      view.layer.cornerRadius = 50;
      view.layer.borderWidth = 2;
      view.layer.borderColor = [UIColor blackColor].CGColor;
      [self.view addSubview:view];
      

1 个答案:

答案 0 :(得分:0)

原因是您重写了drawRect:(CGRect)frame

您正尝试将图层的角半径设置为指定填充颜色的元素。设置masksToBounds达到所需圆角的原因是因为在这里您要告诉视图将所有图层屏蔽到您框架的边界。

在第二个示例UIView中,角半径按预期显示,因为您尚未调整它的图层。

如果您需要 masksToBounds,我建议您采用设置masksToBounds = NO或以不同的方式重新绘制视图界限。因为你有&#34; squiggle&#34; sin曲线,你想要圆角,也许只需创建一个通用的圆角矩形(一个具有所需的圆角半径),然后将其与你已经用sin波创建的路径合并。

Merge multiple CGPaths together to make one path

该链接可帮助您将圆角视图与自定义视图合并,以获得所需的最终结果。

否则......最好将此设置为是。

相关问题