2017-10-14 11 views
0

que je fais une application de dessin, en utilisant rapideattraper le point de graphiques rapides et fondamentaux

l'application de dessin son tout sur les lignes connectées, je en magasin tableau tout premier contact et la dernière touche, donc je veux lorsque la presse utilisateur en vue de la touche de démarrage sera le point plus proche du tableau:

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 

     } 
    } 

} 

le problème est:

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 
       } 
      } 
     } 
    } 

Je veux si l'emplacement touch est autour de n'importe quel point du tableau storePoints, alors le startTouch sera égal au point.

toute idée je serai reconnaissant.

mazen

+0

Quel problème avez-vous? Êtes-vous planter? S'il vous plaît clarifier votre problème. –

+0

@RoboticCat tout va bien, mais je veux quand l'utilisateur appuie sur le point de départ sera le point le plus proche du tableau – mazen

+0

@RoboticCat s'il vous plaît vérifier le commentaire de la fonction touchesBegin. – mazen

Répondre

0

Je ne sais pas si cela est la bonne façon de le faire, mais il est de résoudre le problème de 75%, le code:

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 
      } 
     } 
    } 

pourquoi 75%, parce que: lorsque le tableau avoir trop de points sa obtenir plus difficile d'attraper le point toujours pas pourquoi.

espérons que le code aide quelqu'un.