gpt4 book ai didi

ios - 捕获 swift 和 core 图形的要点

转载 作者:行者123 更新时间:2023-11-30 12:05:14 24 4
gpt4 key购买 nike

我正在使用 swift 制作一个绘图应用程序

绘图应用程序的全部内容都是连接线,我在数组中存储了所有第一次触摸和最后一次触摸,所以我希望当用户在 View 中按下时,开始触摸将是距离数组更近的点:

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 将等于该点。

任何想法我都会感激不尽。

马森

最佳答案

我不知道这是否是正确的方法,但它解决了 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%,因为:当数组有太多点时,它会变得更难捕获点仍然不知道为什么

希望代码对某人有所帮助。

关于ios - 捕获 swift 和 core 图形的要点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com