【问题标题】:How to draw a line in Swift 3如何在 Swift 3 中画一条线
【发布时间】:2018-10-20 18:53:04
【问题描述】:

我希望用户触摸 2 个点,然后在这两个点之间画一条线。这是我目前所拥有的:

func drawline(){
    let context = UIGraphicsGetCurrentContext()
    context!.beginPath()
    context?.move(to: pointA)
    context?.addLine(to: pointB)
    context!.strokePath()
}

pointA 是用户触摸的第一个点,pointB 是第二个点。我得到错误:

thread 1:EXC_BREAKPOINT

提前感谢您的帮助。

【问题讨论】:

    标签: swift drawing


    【解决方案1】:

    要在两点之间画一条线,首先需要从当前的UIView 中获取CGPoints,有几种方法可以实现这一点。我将使用UITapGestureRecognizer 来检测您何时点击。

    另一个步骤是在保存两个点后在两个点之间绘制线,为此,您可以再次使用之前尝试的图形上下文或使用CAShapeLayer

    所以翻译上面的解释我们得到以下代码:

    class ViewController: UIViewController {
    
       var tapGestureRecognizer: UITapGestureRecognizer!
    
       var firstPoint: CGPoint?
       var secondPoint: CGPoint?
    
       override func viewDidLoad() {
           super.viewDidLoad()
    
           tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.showMoreActions(touch:)))
           tapGestureRecognizer.numberOfTapsRequired = 1
           view.addGestureRecognizer(tapGestureRecognizer)
       }
    
       func showMoreActions(touch: UITapGestureRecognizer) {
           let touchPoint = touch.location(in: self.view)
    
           guard let _ = firstPoint else {
               firstPoint = touchPoint
               return
           }
    
           guard let _  = secondPoint else {
               secondPoint = touchPoint
               addLine(fromPoint: firstPoint!, toPoint: secondPoint!)
    
               firstPoint = nil
               secondPoint = nil
    
               return
           }
       }
    
       func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
           let line = CAShapeLayer()
           let linePath = UIBezierPath()
           linePath.move(to: start)
           linePath.addLine(to: end)
           line.path = linePath.cgPath
           line.strokeColor = UIColor.red.cgColor
           line.lineWidth = 1
           line.lineJoin = kCALineJoinRound
           self.view.layer.addSublayer(line)
       }
    }
    

    上面的代码每次选择两个点都会画一条线,你可以根据需要自定义上面的功能。

    希望对你有所帮助。

    【讨论】:

    • 谢谢!我还有一个问题:如何删除用户绘制的线条
    • @CharlesB。不客气,你可以删除之前的CAShapeLayer (和removeFromSuperlayer) 保存引用,这只是一种方法
    • @Victor,我使用了您的 func addline,它可以在单元格内绘制一条线。谢谢
    【解决方案2】:

    在 Swift 4.1 中画线

    class MyViewController: UIViewController {
    
        @IBOutlet weak var imgViewDraw: UIImageView!
    
        var lastPoint = CGPoint.zero
        var red: CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue: CGFloat = 0.0
        var brushWidth: CGFloat = 10.0
        var opacity: CGFloat = 1.0
        var isSwiping:Bool!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        //MARK: Touch events
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            isSwiping    = false
            if let touch = touches.first{
                lastPoint = touch.location(in: imgViewDraw)
            }
        }
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    
            isSwiping = true;
            if let touch = touches.first{
                let currentPoint = touch.location(in: imgViewDraw)
                UIGraphicsBeginImageContext(self.imgViewDraw.frame.size)
                self.imgViewDraw.image?.draw(in: CGRect(x:0, y:0,width:self.imgViewDraw.frame.size.width, height:self.imgViewDraw.frame.size.height))
    
                UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
                UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
    
                UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
                UIGraphicsGetCurrentContext()?.setLineWidth(self.brushWidth)
                UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
                UIGraphicsGetCurrentContext()?.strokePath()
    
    
                self.imgViewDraw.image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                lastPoint = currentPoint
            }
        }
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            if(!isSwiping) {
                // This is a single touch, draw a point
                UIGraphicsBeginImageContext(self.imgViewDraw.frame.size)
                self.imgViewDraw.image?.draw(in: CGRect(x:0, y:0,width:self.imgViewDraw.frame.size.width, height:self.imgViewDraw.frame.size.height))
                UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
                UIGraphicsGetCurrentContext()?.setLineWidth(self.brushWidth)
    
                UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
                UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
                UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
                UIGraphicsGetCurrentContext()?.strokePath()
                self.imgViewDraw.image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
            }
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多