1234567891011121314151617181920212223242526272829303132333435363738 |
- //
- // UIView+rounded.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/11/2.
- //
- import Foundation
- import UIKit
- extension UIView{
-
- func addCorner(_ corners: YDRectCorner, frame: CGRect? = nil) {
- let rect: CGRect = frame ?? self.bounds
- // 绘制路径
- let path = CGMutablePath()
- let topLeftRadius = corners.topLeft
- let topLeftCenter = CGPoint(x: rect.minX + topLeftRadius, y: rect.minY + topLeftRadius)
- path.addArc(center: topLeftCenter, radius: topLeftRadius, startAngle: Double.pi, endAngle: Double.pi * 1.5, clockwise: false)
- let topRightRadius = corners.topRight
- let topRightCenter = CGPoint(x: rect.maxX - topRightRadius, y: rect.minY + topRightRadius)
- path.addArc(center: topRightCenter, radius: topRightRadius, startAngle: Double.pi * 1.5, endAngle: Double.pi * 2, clockwise: false)
- let bottomRightRadius = max(corners.bottomRight, 0)
- let bottomRightCenter = CGPoint(x: rect.maxX - bottomRightRadius, y: rect.maxY - bottomRightRadius)
- path.addArc(center: bottomRightCenter, radius: bottomRightRadius, startAngle: 0, endAngle: Double.pi * 0.5, clockwise: false)
- let bottomLeftRadius = max(corners.bottomLeft, 0)
- let bottomLeftCenter = CGPoint(x: rect.minX + bottomLeftRadius, y: rect.maxY - bottomLeftRadius)
- path.addArc(center: bottomLeftCenter, radius: bottomLeftRadius, startAngle: Double.pi * 0.5, endAngle: Double.pi, clockwise: false)
- path.closeSubpath()
- // 给layer添加遮罩
- let layer = CAShapeLayer()
- layer.path = path
- self.layer.mask = layer
- }
- }
|