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) // 創(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
- (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ā)布平臺,僅提供信息存儲服務。