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

问题描述 投票:0回答:1

我试图画一个圆边的平行四边形。我希望它是可配置的,以便我可以在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)

我在这里遇到的问题是,当使用上面的代码时左上角不是圆的。我很感激任何帮助来实现这一目标。如果有任何其他替代或更简单的方法,请指导我。谢谢!

注意:我使用此代码将Triangle转换为Parallelogram qazxsw poi

ios swift core-graphics
1个回答
0
投票

更改

UIBezierPath Triangle with rounded edges

    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)

© www.soinside.com 2019 - 2024. All rights reserved.