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將等於該點。
任何想法我會感激。
·馬贊
你有什麼問題?你崩潰了嗎?請澄清你的問題。 –
@RoboticCat的每一件事都是好的,但我想當用戶按下查看開始觸摸將從陣列 – mazen
@RoboticCat更接近點請檢查touchesBegin函數的評論。 – mazen