每个角落的边角半径不同

时间:2018-07-02 14:14:01

标签: swift uiview uibezierpath

我尝试用不同大小的角给视图的角。首先,我将所有半径的角都设为最小半径,然后尝试将2个左角更改为更大的尺寸。

enter image description here

func roundCorners(_ corners:UIRectCorner,_ cormerMask:CACornerMask, radius: CGFloat) {
    if #available(iOS 11.0, *){
        self.clipsToBounds = false
        self.layer.cornerRadius = radius
        self.layer.maskedCorners = cormerMask
    }else{
        let rectShape = CAShapeLayer()
        rectShape.bounds = self.frame
        rectShape.position = self.center
        rectShape.path = UIBezierPath(roundedRect: self.bounds,    byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        self.layer.mask = rectShape
    }
}

//

viewMoreView.roundCorners(.bottomLeft, [.layerMinXMinYCorner , .layerMinXMaxYCorner], radius: 13)
viewMoreView.layer.borderWidth = 1
viewMoreView.layer.borderColor = UIColor(red:0.81, green:0.83, blue:0.83, alpha:1).cgColor
viewMoreView.clipsToBounds = true

此代码在左侧更改,但在右侧更改了所有更改(半径)。

使用以下链接,borderRadius看起来不像这张照片!

enter image description here

Different cornerRadius for each corner Swift 3 - iOS

2 个答案:

答案 0 :(得分:0)

我知道这是解决此问题的捷径

    let demoView = UIView(frame: CGRect(x: 0, y: 0, width: vieww.bounds.width, height: vieww.bounds.height))
    demoView.backgroundColor = UIColor.lightGray
    demoView.layer.cornerRadius = 3.0

    let maskPath = UIBezierPath(roundedRect: demoView.bounds,
                                byRoundingCorners: [.topLeft, .bottomLeft],
                                cornerRadii: CGSize(width: 64.0, height: 0.0))

    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.cgPath
    demoView.layer.mask = maskLayer

    vieww.addSubview(demoView)

    let demoViewInner = UIView(frame: CGRect(x: 2, y: 2, width: demoView.bounds.width-4, height: demoView.bounds.height-4))
    demoViewInner.backgroundColor = UIColor.white
    demoViewInner.layer.cornerRadius = 3.0

    let maskPathInner = UIBezierPath(roundedRect: demoViewInner.bounds,
                                byRoundingCorners: [.topLeft, .bottomLeft],
                                cornerRadii: CGSize(width: 60.0, height: 0.0))

    let maskLayerInner = CAShapeLayer()
    maskLayerInner.path = maskPathInner.cgPath
    demoViewInner.layer.mask = maskLayerInner

    demoView.addSubview(demoViewInner)

但是添加和内部视图也看起来像一个解决方案。重塑图层后,添加边框可能不正确。

enter image description here

答案 1 :(得分:0)

可能在视图中添加子层是您可以接受的明确解决方案

let borderLayer = CAShapeLayer()
borderLayer.frame = self.view.bounds
borderLayer.path  = maskPath.cgPath
borderLayer.lineWidth   = 3
borderLayer.strokeColor = UIColor.lightGray.cgColor;
borderLayer.fillColor   = UIColor.clear.cgColor;

viewMoreView.layer.addSublayer(borderLayer)

因为无法将图层边界添加到当前视图,如您所愿。您应该使用子图层。