添加UIView扩展类,可直接复制以下代码,实现视图添加部分圆角,使用代码:
设置上左和上右圆角12度
vipTopView.TB_addCornerBounds(conrners [.topLeft,.topRight],radius:12,tBounds:CGRect(x:0,y:0,width:kScreenWidth-72,height:80))
也可设置边框加圆角,具体代码如下:
extension UIView {
/// 视图添加圆角(可添加部分圆角)
public func TB_addCorner(conrners: UIRectCorner , radius: CGFloat) {
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
public func TB_addCornerBounds(conrners: UIRectCorner , radius: CGFloat, tBounds:CGRect) {
let maskPath = UIBezierPath(roundedRect: tBounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = tBounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
//圆角加边框
public func TB_addCornerBorderLayer(conrners: UIRectCorner , radius: CGFloat,borderColor:UIColor,filColor:UIColor,lineWidth:Int,tBounds:CGRect) {
let maskPath = UIBezierPath(roundedRect: tBounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = tBounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
let borderLayer = CAShapeLayer()
borderLayer.path = maskLayer.path
borderLayer.fillColor = filColor.cgColor
borderLayer.strokeColor = borderColor.cgColor
borderLayer.lineWidth = CGFloat(lineWidth)
borderLayer.frame = self.bounds
self.layer.addSublayer(borderLayer)
}
struct CornerRadii {
var topLeft:CGFloat=0
var topRight:CGFloat=0
var bottomLeft:CGFloat=0
var bottomRight:CGFloat=0
}
//切圆角函数绘制线条
func createPathWithRoundedRect( bounds:CGRect,cornerRadii:CornerRadii) ->CGPath{
let minX = bounds.minX
let minY = bounds.minY
let maxX = bounds.maxX
let maxY = bounds.maxY
//获取四个圆心
let topLeftCenterX = minX + cornerRadii.topLeft
let topLeftCenterY = minY + cornerRadii.topLeft
let topRightCenterX = maxX - cornerRadii.topRight
let topRightCenterY = minY + cornerRadii.topRight
let bottomLeftCenterX = minX + cornerRadii.bottomLeft
let bottomLeftCenterY = maxY - cornerRadii.bottomLeft
let bottomRightCenterX = maxX - cornerRadii.bottomRight
let bottomRightCenterY = maxY-cornerRadii.bottomRight
//虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针
let path :CGMutablePath = CGMutablePath();
//顶 左
path.addArc(center:CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle:CGFloat.pi, endAngle:CGFloat.pi*3/2, clockwise:false)
//顶右
path.addArc(center:CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle:CGFloat.pi*3/2, endAngle:0, clockwise:false)
//底右
path.addArc(center:CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle:0, endAngle:CGFloat.pi/2, clockwise:false)
//底左
path.addArc(center:CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle:CGFloat.pi/2, endAngle:CGFloat.pi, clockwise:false)
path.closeSubpath();
return path;
}
//MARK:添加4个不同尺寸的圆角(可以设置边框)
func TB_addCornerDifferentBorder(cornerRadii:CornerRadii, size:CGSize, borderColor:UIColor, borderWidth:CGFloat) {
let rect = CGRect(origin:CGPoint(x:0, y:0), size: size)
let path = createPathWithRoundedRect(bounds: rect, cornerRadii:cornerRadii)
let shapLayer = CAShapeLayer()
shapLayer.frame = rect
shapLayer.path = path
self.layer.mask = shapLayer
//设置边框
let borderLayer = CAShapeLayer()
borderLayer.path = path
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = borderColor.cgColor
borderLayer.lineWidth = borderWidth
borderLayer.frame = rect
self.layer.addSublayer(borderLayer)
}
}