设置圆角的几种方式

2019-07-05  本文已影响0人  NapoleonY

1. CAShapeLayer+UIBezierPath

func cornerRadius(withRoundedRects roundedRects: UIRectCorner, cornerRadius: CGFloat) {
        let bezierpath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: roundedRects, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = bezierpath.cgPath
        layer.mask = shapeLayer
    }
imageView.cornerRadius(withRoundedRects: UIRectCorner.init(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.topRight.rawValue), cornerRadius: 20)

注意:
UIBezierPath init 方法中的 roundedRect 确定后,即使 view 的bounds.size 变大,实际看到的 view 大小仍然不变,原因是 view 的可见部分即为 UIBezierPath初始化时的 roundedRect 大小,即使即使 bounds.size 变大,可见部分却一直没变。除非每次 view 的 bounds.size 变化后,重新更新 view.mask

2. CAShapeLayer 与 CGMutablePath 设置不同的角不同的圆角值

struct CornerRadii {
    let topLeft: CGFloat
    let topRight: CGFloat
    let bottomLeft: CGFloat
    let bottomRight: CGFloat

    init(topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat) {
        self.topLeft = topLeft
        self.topRight = topRight
        self.bottomLeft = bottomLeft
        self.bottomRight = bottomRight
    }
}
    func CYPathCreateWIthRoundedRect(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
        
        let path = CGMutablePath()
        // 顶左
        path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi / 2 * 3, clockwise: false)
        // 顶右
        path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi / 2 * 3, 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
    }
    
    func createCorner() {
        let cornerRadii = CornerRadii(topLeft: 20, topRight: 1, bottomLeft: 5, bottomRight: 30)
        let path = CYPathCreateWIthRoundedRect(bounds: self.bounds, cornerRadii: cornerRadii)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path
        self.layer.mask = shapeLayer
    }
上一篇下一篇

猜你喜欢

热点阅读