рисовать линию только в одном направлении, начиная с cgpoint

#swift #line #uitouch #cgpoint #uigraphicscontext

#swift #линия #uitouch #cgpoint #uigraphicscontext

Вопрос:

Моя цель swift codes ниже — иметь возможность рисовать прямую линию. Когда пользовательский интерфейс является сенсорным, пользователь может рисовать только на 90 градусов выше начальной точки. Направление уже определено. Таким образом, пользователь может просто нарисовать линию над точкой, к которой прикасаются. Вы можете видеть gif под строкой слева — это то, что делает мой код ниже. Строка справа — это то, чего я хотел бы достичь.

введите описание изображения здесь

 import UIKit



class ViewController: UIViewController{
    
    
    
  
    
    var draw = Canvas()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        [draw].forEach {
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false}
    }
    
    
    
    override func viewDidLayoutSubviews() {
        
        draw.backgroundColor = .clear
        
        
        NSLayoutConstraint.activate ([
            
            
            
            draw.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            
            draw.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.77, constant: 0),
            draw.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
            draw.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
            
            
            
        ])
        
        
        
        
    }
    
    
    
}



struct ColoredLine {
    var color = UIColor.black
    var points = [CGPoint]()
    var width = 5
}





class Canvas: UIView {
    
    
    var strokeColor = UIColor.red
    var strokeWidth = 5
    func undo() {
        _ = lines.popLast()
        setNeedsDisplay()
    }
    
    func clear() {
        lines.removeAll()
        setNeedsDisplay()
    }
    
    var lines = [ColoredLine]()
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        guard let context = UIGraphicsGetCurrentContext() else { return }
        
        lines.forEach { (line) in
            for (i, p) in line.points.enumerated() {
                if i == 0 {
                    context.move(to: p)
                } else {
                    context.addLine(to: p)
                }
            }
            
            context.setStrokeColor(line.color.cgColor)
            context.setLineWidth(CGFloat(line.width))
            context.strokePath()
            context.beginPath()
        }
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var coloredLine = ColoredLine()
        coloredLine.color = strokeColor
        coloredLine.width = strokeWidth
        lines.append(coloredLine)
        
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else { return }
        guard var lastLine = lines.popLast() else { return }
        lastLine.points.append(point)
        lines.append(lastLine)
        setNeedsDisplay()
    }
    
}
  

Ответ №1:

Можно создавать координаты, точки состоят из x и y.

Сохраняя y, и готово.

  • сохранить первую точку для события one touch

       var firstPt: CGPoint?
    
      //  get the first point
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          guard let first = touches.first?.location(in: self) else { return }
    
          // store first as property 
          firstPt = first
      }
      
  • создайте остальные точки, x новых точек получения не имеет значения

       override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
          guard let point = touches.first?.location(in: self), let first = firstPt else { return }
    
          let pointNeeded = CGPoint(x:  first.x ,  y:  point.y)
          //  ... , do as usual
      }
      

и в touchesEnd и touchesCancell , firstPt = nil

Комментарии:

1. можете ли вы добавить свойство к своему ответу?

2. Я добавил свой код из touches end в метод touches end, который вы предоставляете. Однако мой код компилируется, но я ничего не могу нарисовать.

3. ваша ошибка, отлаживайте сами. Я просто выбираю значение y для точек выборки