美文网首页
UIView特殊圆角遮罩处理

UIView特殊圆角遮罩处理

作者: 雪_晟 | 来源:发表于2018-11-19 16:13 被阅读61次
    QQ20181119-161207@2x.png

    如果需要上面的圆角处理,需要自己画遮罩,如果是边框,那么设置为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特殊圆角遮罩处理

          本文链接:https://www.haomeiwen.com/subject/ynrafqtx.html