swift Siri远程触控位置?

e5njpo68  于 2022-10-31  发布在  Swift
关注(0)|答案(3)|浏览(117)

当在Apple TV上的Siri Remote遥控器上收集触摸时,触摸的位置总是报告为相同的位置?

let tapRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.LeftArrow.rawValue),NSNumber(integer: UIPressType.RightArrow.rawValue)];
    self.view.addGestureRecognizer(tapRecognizer)

func tapped(tap :UITapGestureRecognizer){
print(__FUNCTION__)
    print(tap.locationInView(super.view))

}

尝试了locationInView(xxx)中的各种选项,但没有任何结果。还检查了调试器中的tap.,可以看到任何结果。
任何想法,都是它支持的。

wztqucjr

wztqucjr1#

您可以改用touchesBegan方法:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for var touch in touches {
      print(touch.locationInView(self.view))
    }
}
  • 注意-此处显示的语法是历史性的,现在不起作用 *
xxe27gdn

xxe27gdn2#

您可以使用以下方法

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        print("touchesBegan: x: \(location.x)   y: \(location.y)")
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        print("touchesMoved: x: \(location.x)   y: \(location.y)")
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        print("touchesEnded: x: \(location.x)   y: \(location.y)")
    }
}
dldeef67

dldeef673#

这个很好用。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard touches.count == 1, let t = touches.first else { return }
    print(t.location(in: view))
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard touches.count == 1, let t = touches.first else { return }
    print(t.location(in: view))
}

这有点奇怪,但是如果你简单地使用“视图控制器的视图”,它就可以正常工作。
别忘了,当你把手指放下来的时候,苹果遥控器会“复位”。每次你把手指放下来的时候,那就是新的“中心”。

相关问题