如何在垂直方向上移动水平线? 我可以绘制一条直线,我需要使用长按来移动它。 我该怎么做?如何移动线?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
if let touch = touches.first
{
let currentPoint = touch.location(in: self.view)
DrawLine(FromPoint: currentPoint, toPoint: currentPoint)
}
}
func DrawLine(FromPoint: CGPoint, toPoint: CGPoint)
{
UIGraphicsBeginImageContext(self.view.frame.size)
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()
let lineWidth : CGFloat = 5
context?.move(to: CGPoint(x: FromPoint.x, y: FromPoint.y))
context?.addLine(to: CGPoint(x: FromPoint.x + 200, y: FromPoint.y))
context?.setBlendMode(CGBlendMode.normal)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(lineWidth)
context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
context?.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
UIView的什么,如果我有3条线?我如何区分它们并移动所需的? – Vladislav
您可以通过将框架CGRect传递给方法来绘制三条线,并调用要移动的线条实例的moveVertically方法。所以你可以按照你的要求修改上面的代码。 –
上面的代码是绘制一条线并将其沿垂直方向移动。 –