Swift-高效的设置圆角

  • 使用一:
imageView.addHollowOutView(10)
  • 使用二:
imageView.addHollowOutView()
  • 使用三:
imageView.addHollowOutView(nil, nil, [UIRectCorner.topRight,UIRectCorner.bottomRight,UIRectCorner.bottomLeft])
  • 使用四:
imageView.addHollowOutView(nil,UIColor.red)
  • 实现
import UIKit

extension UIView {
    
    /// 快速添加镂空View
    ///
    /// - Parameters:
    ///   - radius: 圆角半径(nil:默认半径)
    ///   - color: 背景颜色(nil:看自己又没有就自己 没有就去取父控件的)
    ///   - corners: 切除部分(nil:默认切4角)
    func addHollowOutView(_ radius: CGFloat? = nil, _ color: UIColor? = nil, _ corners: UIRectCorner? = nil) {
        let rect = bounds
        let backgroundView = UIView(frame: rect) // 创建背景View
        backgroundView.isUserInteractionEnabled = false // 不接收事件 不然会阻挡原始事件触发
        var currentcolor = color ?? backgroundColor // 设置颜色
        if currentcolor == nil { // 如果没设置背景色
            if let superView = self.superview { // 看看父控件是否存在 存在直接用父控件背景色
                currentcolor = superView.backgroundColor
            } else { // 不然给定白色
                currentcolor = UIColor.white
            }
        }
        backgroundView.backgroundColor = currentcolor
        let currentradius:CGFloat = radius ?? rect.size.height*0.5 // 设置圆角半径
        self.addSubview(backgroundView) // 添加遮罩层
        self.bringSubviewToFront(backgroundView) // 放置到最顶层

        let maskLayer = CAShapeLayer()
        maskLayer.fillRule = CAShapeLayerFillRule.evenOdd //  奇偶层显示规则
        let basicPath = UIBezierPath(rect: rect) // 底色
        
        let radii = CGSize(width: currentradius, height: currentradius)
        let currentcorners = corners ?? [UIRectCorner.topRight,UIRectCorner.bottomRight,UIRectCorner.bottomLeft,UIRectCorner.topLeft]
        let maskPath = UIBezierPath(roundedRect: rect, byRoundingCorners: currentcorners, cornerRadii: radii) // 镂空路径
        basicPath.append(maskPath) // 重叠
        maskLayer.path = basicPath.cgPath
        backgroundView.layer.mask = maskLayer
    }
}

Dome

  • oc版
- (void)addRadius:(CGFloat)radius color:(UIColor *__nullable)color corners:(UIRectCorner)corners {
    if (self.currentView && radius == self.hollowOutViewRadius && corners == self.corners && color == self.color) {return;}
//    self.hollowOutViewRadius = radius;
//    self.color = color;
//    self.corners = corners;
    CGRect rect = self.bounds;
    if (self.currentView) {
        [self.currentView removeFromSuperview];
    }
    self.currentView = [[UIView alloc] initWithFrame:rect];
    self.currentView.userInteractionEnabled = NO;
    if (color) {
        self.currentView.backgroundColor = color;
    } else {
        if (self.superview && self.superview.backgroundColor != UIColor.clearColor) {
            self.currentView.backgroundColor = self.superview.backgroundColor;
        } else {
            self.currentView.backgroundColor = CCViewBgColor();
        }
    }
    [self addSubview:self.currentView];
    [self bringSubviewToFront:self.currentView];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.fillRule = kCAFillRuleEvenOdd; //  奇偶层显示规则
    UIBezierPath *basicPath = [UIBezierPath bezierPathWithRect:rect];
    CGSize radii = CGSizeMake(radius, radius);
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    [basicPath appendPath:maskPath];
    maskLayer.path = basicPath.CGPath;
    self.currentView.layer.mask = maskLayer;
    self.currentView.layer.shadowPath = basicPath.CGPath;
}

你可能感兴趣的:(Swift-高效的设置圆角)