iOS开发 矩形切圆,你想怎么切就怎么切

可将以下方法直接添加至UIView的Category内,以便UIView及其子类的使用

先设置frame再使用以下方法

1、整体切圆加边线以及颜色

- (void)borderRadius:(CGFloat)radius width:(CGFloat)width color:(UIColor *)color {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
}

2、胶囊按钮,只切四个的角

//胶囊
- (void)capsuleWithRounded:(CGFloat)rounded {
    
    [self cutWithTopLeftRounded:rounded topRightRounded:rounded bottomRightRounded:rounded bottomLeftRounded:rounded];

}

或者

//胶囊
- (void)capsuleWithRounded:(CGFloat)rounded {
    
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(rounded, rounded)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = self.bounds;
    maskLayer1.path = maskPath1.CGPath;
    self.layer.mask = maskLayer1;
}

3、切指定角

- (void)cutWithTopLeftRounded:(CGFloat)topLeftRounded topRightRounded:(CGFloat)topRightRounded bottomRightRounded:(CGFloat)bottomRightRounded bottomLeftRounded:(CGFloat)bottomLeftRounded{

    UIBezierPath* path = [UIBezierPath bezierPath];
    
    // 开始绘画 - 顺时针
    
    //左上
    [path addArcWithCenter:CGPointMake(topLeftRounded, topLeftRounded) radius:topLeftRounded startAngle:M_PI endAngle:M_PI *3/2 clockwise:YES];
    [path addLineToPoint:CGPointMake(self.width - topRightRounded, 0)];
    
    //右上
    [path addArcWithCenter:CGPointMake(self.width - topRightRounded, topRightRounded) radius:topRightRounded startAngle:M_PI *3/2 endAngle:0 clockwise:YES];
    [path addLineToPoint:CGPointMake(self.width, self.height - bottomRightRounded)];
    
    //右下
    [path addArcWithCenter:CGPointMake(self.width - bottomRightRounded, self.height - bottomRightRounded) radius:bottomRightRounded startAngle:0 endAngle:M_PI *1/2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomLeftRounded, self.height)];
    
    //左下
    [path addArcWithCenter:CGPointMake(bottomLeftRounded, self.height - bottomLeftRounded) radius:bottomLeftRounded startAngle:M_PI *1/2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(0, topLeftRounded)];
    

    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = self.bounds;
    maskLayer1.path = path.CGPath;
    self.layer.mask = maskLayer1;
}

ps:颜色渐变

- (void)gradientColro:(NSArray *)colors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint frame:(CGRect)frame {
    CAGradientLayer *layer = [[CAGradientLayer alloc] init];
    
    __block NSMutableArray *arr = [NSMutableArray array];
    
    [colors enumerateObjectsUsingBlock:^(UIColor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        [arr addObject:(__bridge id)obj.CGColor];
        
    }];
    
    layer.colors = arr;
    layer.startPoint = startPoint;
    layer.endPoint = endPoint;
    layer.frame = CGRectEqualToRect(frame, CGRectZero) ? CGRectMake(0, 0, self.width, self.height) : frame;
    [self.layer addSublayer:layer];
}

参考文章:https://www.jianshu.com/p/6c9aa9c5dd68

你可能感兴趣的:(iOS开发 矩形切圆,你想怎么切就怎么切)