2017-10-14 1 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 
       } 
      } 
     } 
    } 

私が欲しい場所であればtouchは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%、ため、次の場合、配列あまりにも多くのポイントを持っているポイントをキャッチするために難しくなる理由はまだありません

うまくいけばコード助けてください。