UIView特殊圆角遮罩处理

如果需要上面的圆角处理,需要自己画遮罩,如果是边框,那么设置为subLayer即可。
代码如下:

-(void)setQYCornerRadius:(CGFloat)cornerRadius cornerRadius2:(CGFloat)cornerRadius2 lineWith:(CGFloat)lineWidth lineColor:(UIColor *)lineColor{
    
    CGFloat cornerRadii = cornerRadius;
    CGFloat cornerRadii2 = cornerRadius2;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPath];
    [maskPath moveToPoint:CGPointMake(cornerRadii, 0)];
    [maskPath addLineToPoint:CGPointMake(self.width-cornerRadii2, 0)];
    
    [maskPath addArcWithCenter:CGPointMake(self.width-cornerRadii2, cornerRadii2) radius:cornerRadii2 startAngle:-M_PI_2 endAngle:0 clockwise:YES];
    
    [maskPath addLineToPoint:CGPointMake(self.width, cornerRadii)];
    
    [maskPath addArcWithCenter:CGPointMake(self.width-cornerRadii, cornerRadii) radius:cornerRadii startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    [maskPath addLineToPoint:CGPointMake(self.width-cornerRadii , self.height)];
    
    [maskPath addLineToPoint:CGPointMake(cornerRadii , self.height)];
    
    [maskPath moveToPoint:CGPointMake(cornerRadii, self.height)];
    
    [maskPath addArcWithCenter:CGPointMake(cornerRadii, cornerRadii) radius:cornerRadii startAngle:0.5 * M_PI endAngle: 1.5 *M_PI clockwise:YES];
    [maskPath addLineToPoint:CGPointMake(cornerRadii, 0)];
    
  
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    
    //如果是遮罩的话下面两行代码不起作用。
    maskLayer.strokeColor = lineColor.CGColor;
    
    maskLayer.lineWidth = lineWidth;
    //设置大小
    maskLayer.frame = self.bounds;
    
    //设置图形样子
    maskLayer.path = maskPath.CGPath;

    //如果是添加子layer,不用设置mask遮罩,只用添加subLayer即可,同时可以设置strokeColor,以及lineWidth;
    self.layer.mask = maskLayer;
}

你可能感兴趣的:(UIView特殊圆角遮罩处理)