首页 > 解决方案 > 快速获取触摸的各种坐标

问题描述

我试图在 swift 4 的 UIView 中触摸时获取各个点的坐标。我已经看过关于类似问题的另一篇文章,但该代码只允许注册第一次触摸。我会很感激一些帮助。谢谢。

标签: swifttouchesbegan

解决方案


所以,我也能够找到我的问题的答案:我在 UIViewController 中使用下面的代码在不同位置触摸时获取一个二维整数数组。感谢所有的帮助。

var positionArray = Array(repeating: Array(repeating: 0, count: 2), count: 10)
    var counter = 0
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.view)
            let locx = Int(position.x)
            let locy = Int(position.y)
                positionArray[counter] = [locx, locy]
                print(positionArray[counter])
                counter = counter + 1
        }
    }

推荐阅读