使用UIBezierPath 给UIButton 画圆角

关于UIBezierPath


网上有很多相关的内容,在此不再累述,自行百度或谷歌

给UIButton 画圆角


1、创建 UIBezierPath

// byRoundingCorners 设置需要切圆角的属性 
UIBezierPath *roundPath = [UIBezierPath bezierPathWithRoundedRect:self.drawRec.frame byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];

2、创建 CAShapeLayer

  CAShapeLayer *shapeLayer = [CAShapeLayer layer];
   shapeLayer.frame = self.yourButton.frame;
   shapeLayer.path = roundPath.CGPath;
   shapeLayer.lineWidth = 2.f;
   shapeLayer.strokeColor = [UIColor redColor].CGColor;
   shapeLayer.fillColor = [UIColor clearColor].CGColor;

3、使用

// 使用二者的差异
    /**
     边线颜色使用的是yourButton.BackgroundColor
     设置 strokeColor 无用,
     */
    self.yourButton.layer.mask = shapeLayer;
    
    /**
     边线颜色是 strokeColor 的颜色
     */
    [self.yourButton.layer addSublayer:shapeLayer];

你可能感兴趣的:(使用UIBezierPath 给UIButton 画圆角)