2017-10-14 195 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%,原因是:當陣列有太多的點它更難以趕上點仍然不爲什麼

希望代碼可以幫助某人。