Swift-高效的設置圓角

  • 使用一:
imageView.addHollowOutView(10)
  • 使用二:
imageView.addHollowOutView()
  • 使用三:
imageView.addHollowOutView(nil, nil, [UIRectCorner.topRight,UIRectCorner.bottomRight,UIRectCorner.bottomLeft])
  • 使用四:
imageView.addHollowOutView(nil,UIColor.red)
  • 實現(xiàn)
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) // 創(chuàng)建背景View
        backgroundView.isUserInteractionEnabled = false // 不接收事件 不然會阻擋原始事件觸發(fā)
        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 //  奇偶層顯示規(guī)則
        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; //  奇偶層顯示規(guī)則
    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;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 方法一:在代碼中,通過 cornerRadius 屬性設置 方法二:在 storyboard 或 xib 中通過 ...
    Sparkle_S閱讀 12,597評論 0 2
  • “春雨貴如油”,周六下了一天雨,周日被我媽迫不及待地喊回家---種花生。 在我上初三時,舉家搬遷,開始了城市生活。...
    怒放的葉子閱讀 1,518評論 3 7
  • 在上Coursera"設計原理"這門課的時候,第一節(jié)課作業(yè)要求舉出違背distributed cognition ...
    711child閱讀 2,677評論 2 2
  • 石英石,通常我們說的石英石是一種由90%以上的石英晶體加上樹脂及其他微量元素人工合成的一種新型石材。它是通過特殊的...
    藝術吧閱讀 1,880評論 0 0

友情鏈接更多精彩內容