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