向 UIBezierPath addArc 函数添加箭头端
Add an arrow end to the UIBezierPath addArc function
我正在尝试实现以下结果,直到 UIBezierPath 弧结束:
我找到了一个类似的答案,但是它没有使用addArc函数,所以我无法设置开始和结束角度并且写在obj-c上。
extension UIBezierPath {
func addArrow(start: CGPoint, end: CGPoint, pointerLineLength: CGFloat, arrowAngle: CGFloat) {
self.move(to: start)
self.addLine(to: end)
let startEndAngle = atan((end.y - start.y) / (end.x - start.x)) + ((end.x - start.x) < 0 ? CGFloat(Double.pi) : 0)
let arrowLine1 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle + arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle + arrowAngle))
let arrowLine2 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle - arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle - arrowAngle))
self.addLine(to: arrowLine1)
self.move(to: end)
self.addLine(to: arrowLine2)
}
}
用法:
path.addArc(withCenter: center, radius: radius,
startAngle: angle1Inradians, endAngle: angle2Inradians,
clockwise: false)
let pathCurrentPoint = path.currentPoint
let x = center.x + radius * cos(angle1Inradians)
let y = center.y - radius * sin(angle2Inradians)
let point = CGPoint(x: x , y: y )
path.addArrow(start: pathCurrentPoint, end: point, pointerLineLength: 10, arrowAngle: CGFloat(Double.pi / 4))
let layer = CAShapeLayer()
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 2
layer.strokeColor = UIColor.orange.cgColor
layer.path = path.cgPath
self.layer.addSublayer(layer)
我正在尝试实现以下结果,直到 UIBezierPath 弧结束:
我找到了一个类似的答案
extension UIBezierPath {
func addArrow(start: CGPoint, end: CGPoint, pointerLineLength: CGFloat, arrowAngle: CGFloat) {
self.move(to: start)
self.addLine(to: end)
let startEndAngle = atan((end.y - start.y) / (end.x - start.x)) + ((end.x - start.x) < 0 ? CGFloat(Double.pi) : 0)
let arrowLine1 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle + arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle + arrowAngle))
let arrowLine2 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle - arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle - arrowAngle))
self.addLine(to: arrowLine1)
self.move(to: end)
self.addLine(to: arrowLine2)
}
}
用法:
path.addArc(withCenter: center, radius: radius,
startAngle: angle1Inradians, endAngle: angle2Inradians,
clockwise: false)
let pathCurrentPoint = path.currentPoint
let x = center.x + radius * cos(angle1Inradians)
let y = center.y - radius * sin(angle2Inradians)
let point = CGPoint(x: x , y: y )
path.addArrow(start: pathCurrentPoint, end: point, pointerLineLength: 10, arrowAngle: CGFloat(Double.pi / 4))
let layer = CAShapeLayer()
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 2
layer.strokeColor = UIColor.orange.cgColor
layer.path = path.cgPath
self.layer.addSublayer(layer)