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
- (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;
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。