Swift UIView 绘制对角线一侧和圆角

问题描述

我正在用对角线绘制一些视图

像这样:

enter image description here

enter image description here

enter image description here

代码

    let layerWidth = layer.frame.width
    let bezierPath = UIBezierPath()
    let pointA = CGPoint(x: 0,y: 44)
    let pointB = CGPoint(x: 150,y: 0)
    let pointC = CGPoint(x: layerWidth,y: 0)
    let pointD = CGPoint(x: layerWidth,y:44)
    bezierPath.move(to: CGPoint(x: pointA.x,y: pointA.y) )
    bezierPath.addLine(to: CGPoint(x: pointB.x,y: pointB.y))
    bezierPath.addLine(to: pointC)
    bezierPath.addLine(to: pointD)
    bezierPath.close()
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = bezierPath.cgPath
    layer.addSublayer(shapeLayer)

如你所见,一切都很好,但我想让角落变圆

像这样:

enter image description here

enter image description here

enter image description here

我怎样才能达到这个结果?

添加: 并且收到结果后,我想把这个表单设置为另一个视图的掩码,但是有一个问题:第二个视图有边框,如果我设置自己的表单,我会丢失这个边框的一部分

代码

layer.mask = shapeLayer

错误结果:

enter image description here

解决方法

Okey 是我创立的方式 - https://stackoverflow.com/a/60035279/676205

只写 4 点,如:

        let grayPath = UIBezierPath()
        addArcs(
            to: grayPath,pointsAndRadii: [
                (point: CGPoint(x: 0,y: 0),radius: 2),(point: CGPoint(x: self.bounds.width - 104,radius: 10),(point: CGPoint(x: self.bounds.width,y: self.bounds.height),(point: CGPoint(x: 104,radius: 6),])
        grayPath.close()

下一组掩码:

let shapeLayer = CAShapeLayer()
shapeLayer.path = grayPath.cgPath
layer.mask = shapeLayer

如果你有更高效的方法或者更好的性能,就写吧)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...