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
Quel problème avez-vous? Êtes-vous planter? S'il vous plaît clarifier votre problème. –
@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
@RoboticCat s'il vous plaît vérifier le commentaire de la fonction touchesBegin. – mazen