掌握快速而核心的图形

掌握快速而核心的图形

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

绘图应用程序有关连接线的所有内容,
我在数组中存储了所有的第一次触摸和最后一次触摸,所以我想当用户按下视图时,开始触摸将是数组中的较近点:

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 - 掌握快速而核心的图形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46747479/

10-09 01:48