我使用UIBezierPath绘制一条线。我想拉伸它,但它不起作用。我究竟做错了什么?如何更改线的起点和终点?
let line = CAShapeLayer()
let linePath = UIBezierPath()
func DrawLine()
{
linePath.move(to: to: CGPoint(x: 100, y: 100)
linePath.addLine(to: CGPoint(x: self.view.frame.width - 100, y: 100))
line.path = linePath.cgPath
line.strokeColor = UIColor.red.cgColor
line.lineWidth = 1
line.lineJoin = kCALineJoinRound
self.view.layer.addSublayer(line)
}
override func viewDidLoad()
{
super.viewDidLoad()
DrawLine()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
line.frame.origin.x -=10
line.frame.size.width += 10
}
答案 0 :(得分:0)
尝试使用属性存储要更改的点,并在需要时重新创建路径。
class FileViewController: UIViewController {
let line = CAShapeLayer()
var point1 = CGPoint.zero
var point2 = CGPoint.zero
func DrawLine() {
point1 = CGPoint(x: 100, y: 100)
point2 = CGPoint(x: self.view.frame.width - 100, y: 100)
let linePath = UIBezierPath()
linePath.move(to: point1)
linePath.addLine(to: point2)
line.path = linePath.cgPath
line.strokeColor = UIColor.red.cgColor
line.lineWidth = 1
line.lineJoin = kCALineJoinRound
self.view.layer.addSublayer(line)
}
override func viewDidLoad() {
super.viewDidLoad()
DrawLine()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
point1.x -= 10
point2.x += 10
let linePath = UIBezierPath()
linePath.move(to: point1)
linePath.addLine(to: point2)
line.path = linePath.cgPath
}
}