这段代码与我所期望的一样,它画了一条弧线,然后在该弧线的顶部添加了一条50点宽的线。它画了一条弧线,然后从弧线的顶部添加一条50点宽的线。
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
这段代码忽略了 addLine
用于添加一条50点宽的线,并且只是在第一条弧线的顶部开始第二条弧线。
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: centerX + 50, y: centerY), radius: radius1, startAngle: (3 * .pi)/2, endAngle: .pi, clockwise: false)
在这第二段代码中,如果我注释掉下面的 addLine
代码。如果我把addLine代码改为添加300,我得到了完全相同的输出。像素 点,而不是50点。addLine代码被忽略了,我得到了两条弧线,在第一条弧线结束和第二条弧线开始之间没有一条线。
有什么建议吗?非常感谢
你说的。
有了这第二段代码,如果我注释掉addLine代码,我得到的结果是一样的。
是的,当你在一个现有的路径上添加一个弧线时,它会自动地从 currentPoint
到这第二条弧线的开始。如果你不想让它在中间加上这条线,你需要做一个 move(to:)
如果你不想要中间的那条线,就在你的路径中的第二条弧线开始的地方。或者创建两条路径,每条弧线一条,然后分别描边。
如果我把路径中的
addLine
代码添加300像素而不是50像素。
这不太合理,我无法重现这种行为。例如,当我把第二条弧线移动50pt时,我得到的是这样的结果(我会把这个笔画做成动画,这样你就可以看到发生了什么)。
但这是我将线条移动300pt时的结果(但第二条弧线与第一条弧线的距离只有50pt)。
很明显,如果你不仅把线变长300pt,而且把第二条弧线的中心也移动300pt,那么它就会像第一个例子一样(除了距离更远)。
然而,如果我把你的 addLine(to:)
与 move(to:)
,那么你就不会得到它们中间的那条线。
FWIW,在所有这些例子中,我都不知道你用什么来做什么 myX
和 myY
所以我用了第一条弧线左边的一个点。显然,如果你不想要那条额外的线,可以将它移到 myX
和 myY
到第一段弧线的开始(或者干脆把它完全注释出来)。
我不知道你的代码有什么问题,但有什么我已经试过了......它可能会帮助你,这就是为什么发布它。
@IBDesignable
class CustomBg:UIView {
private lazy var curvedLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.yellow.cgColor
shapeLayer.lineWidth = 4
shapeLayer.strokeColor = UIColor.green.cgColor
return shapeLayer
}()
//MARK:- inializer
override init(frame: CGRect) {
super.init(frame: frame)
layer.addSublayer(curvedLayer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
layer.addSublayer(curvedLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let centerX = 100
let centerY = 100
let radius1 = CGFloat( 50.0)
let path = UIBezierPath()
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle:0, endAngle: (2 * .pi), clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 150, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: Int(path.currentPoint.x + radius1) , y: centerY), radius: radius1 , startAngle: .pi, endAngle: (3 * .pi ), clockwise: true)
curvedLayer.path = path.cgPath
}
}