// // CGMutablePath+SPAdd.swift // ShortPlay // // Created by 曾觉新 on 2025/4/14. // import UIKit /** 圆角大小 */ struct SPCirculars { var topLeft:CGFloat = 0 var topRight:CGFloat = 0 var bottomLeft:CGFloat = 0 var bottomRight:CGFloat = 0 public static let zero = SPCirculars(topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0) public init(topLeft: CGFloat, topRight:CGFloat, bottomLeft:CGFloat, bottomRight:CGFloat) { self.topLeft = topLeft self.topRight = topRight self.bottomLeft = bottomLeft self.bottomRight = bottomRight } static func ==(v1:SPCirculars, v2:SPCirculars) -> Bool { return v1.bottomLeft == v2.bottomLeft && v1.bottomRight == v2.bottomRight && v1.topLeft == v2.topLeft && v1.topRight == v2.topRight } static func !=(v1:SPCirculars, v2:SPCirculars) -> Bool { return !(v1 == v2) } } extension CGMutablePath { func addRadiusRectangle(_ circulars: SPCirculars, rect: CGRect) { let minX = rect.minX let minY = rect.minY let maxX = rect.maxX let maxY = rect.maxY //获取四个圆心 let topLeftCenterX = minX + circulars.topLeft let topLeftCenterY = minY + circulars.topLeft let topRightCenterX = maxX - circulars.topRight let topRightCenterY = minY + circulars.topRight let bottomLeftCenterX = minX + circulars.bottomLeft let bottomLeftCenterY = maxY - circulars.bottomLeft let bottomRightCenterX = maxX - circulars.bottomRight let bottomRightCenterY = maxY - circulars.bottomRight //顶 左 addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: circulars.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: false) //顶右 addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: circulars.topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: 0, clockwise: false) //底右 addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: circulars.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false) //底左 addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: circulars.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: false) closeSubpath(); } }