【问题标题】:draw line only in one direction starting with cgpoint仅从 cgpoint 开始在一个方向上画线
【发布时间】:2020-10-13 02:31:25
【问题描述】:

我下面的 swifts 代码目标是能够画一条直线。当 uiview 被触摸时,用户只能在初始点上方绘制 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()
    }
    
}

【问题讨论】:

    标签: swift line uitouch cgpoint uigraphicscontext


    【解决方案1】:

    制作坐标即可,Points由x和y组成。

    保持y,完成。

    • 为一个触摸事件保留第一个点

        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
        }
      

    touchesEndtouchesCancellfirstPt = nil

    【讨论】:

    • 您可以将属性添加到您的答案中吗?
    • 我将 touches end 中的代码添加到您提供的 touches end 方法中。但是我的代码正在编译,但我无法绘制任何东西。
    • 你的错,自己调试。我只是选择样本点的 y val
    猜你喜欢
    • 2017-09-29
    • 2013-10-23
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多