【问题标题】:Draw a round edge parallelogram with some shadow offset绘制带有一些阴影偏移的圆边平行四边形
【发布时间】:2019-04-10 18:54:36
【问题描述】:

我正在尝试绘制一个带有圆边的平行四边形。我希望它是可配置的,以便我可以在 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

【问题讨论】:

    标签: ios swift core-graphics


    【解决方案1】:

    改变

        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)
    

    结果:

    【讨论】:

    • 那是完美的。现在我知道我做错了什么。感谢您的所有帮助马特
    • 不幸的是,stackoverlfow 不允许我投票给你,因为我是这个平台的新手。对此感到抱歉。
    • 非常感谢这个(问题和答案)
    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多