如何检测 Swift 3 中可见的 UITouch 位置

How to detect UITouch location visible in Swift 3

我正在尝试检测 UITouch 事件 visible.When 触摸事件 begins.Currently,我正在使用下面的代码检测触摸 location.From 下面的代码,我可以打印触摸location.Any,帮助将不胜感激。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if let touch = touches.first {
        let position :CGPoint = touch.location(in: view)
        print(position.x)
        print(position.y)
    }
}

注意:我不是要画线或任何类似绘图的东西app.I,只是想看到触摸事件visibly.When发生。

提前致谢。

如果你想要在用户点击屏幕时出现一个圆圈或其他东西,试试这个:

var touchIndicators: [UIView] = []
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: view)
        let touchIndicator = UIView(frame: CGRect(x: location.x - 10, y: location.y - 10, width: 20, height: 20))
        touchIndicator.alpha = 0.5
        touchIndicator.backgroundColor = UIColor.red
        touchIndicator.layer.cornerRadius = 10
        self.view.addSubview(touchIndicator)
        touchIndicators.append(touchIndicator)
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for indicator in touchIndicators {
        indicator.removeFromSuperview()
    }
    touchIndicators = []
}

非常简单。当用户触摸屏幕时添加圆形视图,并在用户抬起 his/her 手指时移除它们。您也可以使用 UITapGestureRecognizer.