当我尝试使用以下代码绘制如下形状时,该形状仅显示半笔划线。
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))
有什么建议吗?谢谢!
最佳答案
这是我尝试后得到的;希望对你有帮助
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
}
输出:
关于ios - UIBezierPath 只有半个笔画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799343/