ios - UIBezierPath 只有半个笔画

标签 ios swift uibezierpath

当我尝试使用以下代码绘制如下形状时,该形状仅显示半笔划线。

enter image description here

let shape = SKShapeNode()
            shape.path = createPointerPath().cgPath
            shape.position = CGPoint(x: frame.midX, y: frame.midY)
            shape.fillColor = UIColor(red: 1.0, green: 0.439, blue: 0.678, alpha: 1.0)
            shape.strokeColor = .white
            shape.lineWidth = 2
            addChild(shape)

创建形状:

func createPointerPath() -> UIBezierPath {

    let pointerPath = UIBezierPath()
    pointerPath.move(to: CGPoint(x:0, y:0))

    pointerPath.addCurve(to: CGPoint(x:0, y:100), controlPoint1: CGPoint(x:-10, y:0), controlPoint2: CGPoint(x:-50, y:100))
    pointerPath.addCurve(to: CGPoint(x:0, y:0), controlPoint1: CGPoint(x:50, y:100), controlPoint2: CGPoint(x:10, y:0))

    pointerPath.stroke()        
    pointerPath.close()
    return pointerPath
}

如果我将 x 设置为 10,它会显示另一条 half stroe 线,但形状没有闭合。

    pointerPath.addCurve(to: CGPoint(x:10, y:0), controlPoint1: CGPoint(x:50, y:100), controlPoint2: CGPoint(x:10, y:0))

enter image description here

有什么建议吗?谢谢!

最佳答案

这是我尝试后得到的;希望对你有帮助

    let v : UIView = UIView.init(frame: CGRect.init(x: 50, y: 50, width: 200, height: 200))
    v.layer.borderWidth = 2.0
    let shape = CAShapeLayer()
    shape.path = createPointerPath().cgPath
    shape.position = CGPoint(x: frame.midX, y: frame.midY)
    shape.fillColor = UIColor(red: 1.0, green: 0.439, blue: 0.678, alpha: 1.0).cgColor
    shape.strokeColor = UIColor.white.cgColor
    shape.lineWidth = 2
    v.layer.addSublayer(shape)
    self.addSubview(v)

这与您提供的功能相同:

func createPointerPath() -> UIBezierPath {

    let pointerPath = UIBezierPath()
    pointerPath.move(to: CGPoint(x:0, y:0))

    pointerPath.addCurve(to: CGPoint(x:0, y:100), controlPoint1: CGPoint(x:-10, y:0), controlPoint2: CGPoint(x:-50, y:100))
    pointerPath.addCurve(to: CGPoint(x:0, y:0), controlPoint1: CGPoint(x:50, y:100), controlPoint2: CGPoint(x:10, y:0))

    pointerPath.stroke()
    pointerPath.close()
    return pointerPath
}

输出:

Output of Above Code

关于ios - UIBezierPath 只有半个笔画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799343/

相关文章:

ios - 如何在选择器 View 中填充 "ArrayYear"?

ios - 在调用 resignFirstResponder 之前,UITextField 不会自动调整宽度以适应内容?

swift - UIBezierPath,如何绘制 "QuadCurve"

ios - 在 UITableViewCell 中的不同大小的 UIView 上放置一个 mask 层

ios - 如何添加圆到矩形? UIBezierPath

xcode - 如何将私有(private)程序分发到 100 多个设备?

ios - 如何将标签结果保留在 secondViewController 上?

ios - 隐藏导航栏,将剩余 View 移至状态栏下方

ios - SWIFT:创建具有不同原型(prototype)样式的 tableView

iOS swift : Calling a singleton method 3 times causes app to crash