2017-10-14 196 views
0

我想提出一个绘图应用程序,使用快捷渔获量迅速和核心图形

绘图应用它的所有关于连接线, 我在阵列存储中的所有第一触摸和最后的触摸,所以我想点当考虑到用户按下启动触摸将来自阵列的更近的点:

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet weak var drawingPlace: UIImageView! 

    var startTouch : CGPoint? 
    var finalTouch : CGPoint? 
    var storePoints : [CGPoint] = [] // this will store the first touch and the last touch 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     let location = touch?.location(in: drawingPlace) // 
     if startTouch == nil{ 
     startTouch = touch?.location(in: drawingPlace) 
      storePoints.append(startTouch!) 
     }else{ 
      for point in storePoints{ 

       if location == point { // i want to say if location == point or near the point 
        startTouch = point // so the start touch equal the point 
       } 
      } 
     } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     finalTouch = touch?.location(in: drawingPlace) 
     storePoints.append(finalTouch!) 
     if startTouch != nil{ 
      UIGraphicsBeginImageContext(drawingPlace.frame.size) 
      drawingPlace.image?.draw(in: drawingPlace.bounds) 
      let context = UIGraphicsGetCurrentContext() 

      context?.beginPath() 
      context?.move(to: startTouch!) 
      context?.addLine(to: finalTouch!) 
      context?.strokePath() 

      let img = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      drawingPlace.image = img 

     } 
    } 

} 

问题是:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     let location = touch?.location(in: drawingPlace) // 
     if startTouch == nil{ 
     startTouch = touch?.location(in: drawingPlace) 
      storePoints.append(startTouch!) 
     }else{ 
      for point in storePoints{ 

       if location == point { // i want to say if location == point or near the point 
        startTouch = point // so the start touch equal the point 
       } 
      } 
     } 
    } 

我想如果位置触摸位于storePoints数组的任何点上,那么startTouch将等于该点。

任何想法我会感激。

·马赞

+0

你有什么问题?你崩溃了吗?请澄清你的问题。 –

+0

@RoboticCat的每一件事都是好的,但我想当用户按下查看开始触摸将从阵列 – mazen

+0

@RoboticCat更接近点请检查touchesBegin函数的评论。 – mazen

回答

0

我不知道这是否是这样做的正确的方式,但它是解决问题的75%,代码:

func catchPoint(points : [CGPoint] , location : CGPoint){ 
     let qal = points[0].x - points[0].y 
     for point in points{ 

      let x = location.x - point.x 
      let y = location.y - point.y 

      if abs(x) + abs(y) < abs(qal){ 
       startTouch = point 
      } 
     } 
    } 

为什么75%,原因是:当阵列有太多的点它更难以赶上点仍然不为什么

希望代码可以帮助某人。