绘制带有阴影偏移的圆边平行四边形

时间:2019-04-10 18:54:36

标签: ios swift core-graphics

我正在尝试绘制带有圆形边缘的平行四边形。我希望它是可配置的,以便可以将其中一个垂直边缘设为90度。

As you can see in the image that topLeft corner is not rounded with code below

func getParallelogram(width: CGFloat, height: CGFloat, radius: CGFloat) -> CGPath {
        // Points of the parallelogram
        var points = [
            CGPoint(x: width * 0.05, y: 0),
            CGPoint(x: width , y: 0),
            CGPoint(x: width - width * 0.05, y: height),
            CGPoint(x: 0, y: height)
        ]

        let point1 = points[0]
        let point2 = points[1]
        let point3 = points[2]
        let point4 = points[3]

        let path = CGMutablePath()

        path.move(to: point1)
        path.addArc(tangent1End: point1, tangent2End: point2, radius: radius)
        path.addArc(tangent1End: point2, tangent2End: point3, radius: radius)
        path.addArc(tangent1End: point3, tangent2End: point4, radius: radius)
        path.addArc(tangent1End: point4, tangent2End: point1, radius: radius)
        return path
    }

用法:

let customView = UIView(frame: CGRect(x: 20, y: 50, width: 320, height: 300))
customView.backgroundColor = UIColor.clear
view.addSubview(customView)

let shape = CAShapeLayer()
shape = UIColor.lightGray.cgColor
shape.path = getParallelogram(width: 200, height: 80, radius: 5)
shape.position = CGPoint(x: 10, y: 10)
shape.layer.addSublayer(triangle)

我在这里面临的问题是使用上述代码时左上角不是圆形的。我将不胜感激,以实现这一目标。如果还有其他替代方法或更简单的方法,请也指导我。谢谢!

注意:我正在使用此代码将“三角形转换为平行四边形UIBezierPath Triangle with rounded edges

1 个答案:

答案 0 :(得分:0)

更改

    path.move(to: point1)
    path.addArc(tangent1End: point1, tangent2End: point2, radius: radius)
    path.addArc(tangent1End: point2, tangent2End: point3, radius: radius)
    path.addArc(tangent1End: point3, tangent2End: point4, radius: radius)
    path.addArc(tangent1End: point4, tangent2End: point1, radius: radius)

    path.move(to: point1)
    path.addArc(tangent1End: point2, tangent2End: point3, radius: radius)
    path.addArc(tangent1End: point3, tangent2End: point4, radius: radius)
    path.addArc(tangent1End: point4, tangent2End: point1, radius: radius)
    path.addArc(tangent1End: point1, tangent2End: point2, radius: radius)

结果:

enter image description here