没有uigesturerecognizer的ios触摸位置

时间:2017-08-30 09:41:07

标签: ios uiview touch uiwindow

如果没有uigesturerecognizer,如何在uiwindow或uiview上获取触摸位置, 目的是以极低的延迟获得触摸位置,因为调度员可以延迟触发射击事件

2 个答案:

答案 0 :(得分:0)

您需要继承UIView并覆盖UIResponderUIView的超类)方法touchesBegan(_:with:)。请参阅方法here的文档。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // Get the touch location in THIS VIEW's coordinate system:
        let locationInThisView = touch.location(in: self)

        // Get the touch location in the WINDOW's coordinate system
        let locationInWindow = touch.location(in: nil)
    }
}

另外,请确保您的视图的属性isUserInteractionEnabled设置为true

答案 1 :(得分:0)

UIView 子类中覆盖以下方法。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let point =  touches.first?.location(in: self)
    print("location: ", point ?? CGPoint.zero)
}