每个角的不同 CornerRadius 到 Border

Posted

技术标签:

【中文标题】每个角的不同 CornerRadius 到 Border【英文标题】:Different CornerRadius to Border for each corner 【发布时间】:2018-07-02 14:14:01 【问题描述】:

我尝试为不同大小的视图角赋予角。首先,我给所有关于最小半径的拐角拐角,然后我尝试用更大的尺寸改变 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

【问题讨论】:

【参考方案1】:

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

    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)

然而,添加和内部视图看起来也是一种解决方案。图层重塑后,添加边框可能不是正确的方法。

【讨论】:

此解决方案与我的问题不兼容。【参考方案2】:

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

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)

因为向当前视图添加图层边框不能如您所愿。你应该使用子层。

【讨论】:

以上是关于每个角的不同 CornerRadius 到 Border的主要内容,如果未能解决你的问题,请参考以下文章

iOS 琐碎点------切某个或某几个角的圆角

根据子类中的 UIButton 大小设置cornerRadius 的最佳位置?

H5C3--圆角

钻石般的 UIView

WPF 边框对象边框 CornerRadius 与边框背景 CornerRadius 不同

定义渐变/形状/角的 XML 父样式