美文网首页
swift 设置view不同位置任意大小圆角

swift 设置view不同位置任意大小圆角

作者: _TLB谁用了这个昵称 | 来源:发表于2020-09-14 18:01 被阅读0次

效果图:


IMG224.png

话不多说,上代码:

extension UIView{
    /**
       设置view指定位置指定大小圆角
     */
    func setCornersWith(_topLeft:CGFloat,topRight:CGFloat,bottomRight:CGFloat,bottomLeft:CGFloat){
        if topLeft == 0&&topRight == 0&&bottomRight == 0&&bottomLeft == 0{
            return
        }
        let maxX =self.bounds.maxX
        let maxY =self.bounds.maxY
        //获取中心点
        lettopLeftCenter =CGPoint.init(x: topLeft, y: topLeft)
        lettopRightCener =CGPoint.init(x: maxX-topRight, y: topRight)
        letbottomRightCenter =CGPoint.init(x: maxX-bottomRight, y: maxY-bottomRight)
        letbottomLeftCenter =CGPoint.init(x: bottomLeft, y: maxY-bottomLeft)
        letshaperLayer =CAShapeLayer.init()
        letmutablePath =CGMutablePath.init()
        //左上
        mutablePath.addArc(center: topLeftCenter, radius: topLeft, startAngle: .pi, endAngle: .pi/2*3, clockwise:false)
        //右上
        mutablePath.addArc(center: topRightCener, radius: topRight, startAngle: .pi/2*3, endAngle:0, clockwise:false)
        //右下
        mutablePath.addArc(center: bottomRightCenter, radius: bottomRight, startAngle:0, endAngle:CGFloat(Double.pi/2), clockwise:false)
        //左下
        mutablePath.addArc(center: bottomLeftCenter, radius: bottomLeft, startAngle: .pi/2, endAngle: .pi, clockwise:false)
        shaperLayer.path= mutablePath
        self.layer.mask= shaperLayer
    }
}

使用:

let view = UIView.init(frame: CGRect.init(x: 10, y: 10, width: 200, height: 200))
view.setCornersWith(10, topRight: 20, bottomRight: 5, bottomLeft: 0)

相关文章

网友评论

      本文标题:swift 设置view不同位置任意大小圆角

      本文链接:https://www.haomeiwen.com/subject/iqozektx.html