如何在 tvOS 中添加检测按钮按下?
How to add detect button presses in tvOS?
我已经按照此 tutorial 进行操作,一切正常。我遇到的唯一问题是我不知道如何检测按钮何时被按下?
提前致谢
按钮按下类似于 UITouch 事件。查看 UIResponder header 中的 UIPressEvent 回调。 UIViewControllers 是响应链的一部分,因此您可以在视图控制器中添加回调,类似于:
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event {
UIPress *anyPress = [presses anyObject];
// handle press event
}
根据 Apple Docs
,您可以像 iOS 一样向 UIButton 添加操作
An instance of the UIButton class implements a button on the touch
screen. A button intercepts touch events and sends an action message
to a target object when tapped. Methods for setting the target and
action are inherited from UIControl. This class provides methods for
setting the title, image, and other appearance properties of a button.
By using these accessors, you can specify a different appearance for
each button state.
此处教程的作者,向基于 TVML 的应用程序添加交互性的方法是在有问题的 DOM 元素上使用 addEventListener。您可以通过在创建期间保留对元素的引用或使用 getElementById 或其他类似的 JavaScript DOM 技术来找到 DOM 元素。另外,我应该提到我已经在提到的教程中添加了一个 "Part 2",其中包括这个作为它的主要焦点。
这是一个如何在 JS 中执行此操作的示例,假设我的DOMElement 是一个将您的按钮引用为 DOM 元素的变量。
myDOMElement.addEventListener("select", function() { alert("CLICK!") }, false);
当然,我有更多关于教程的信息,所以请随意 check that out。
您不应该在 tvOS 上使用 "UIControlEvents.TouchUpInside",因为这不会达到您的预期:您可能想改用 "UIControlEvents.PrimaryActionTriggered"。
TouchUpInside 由实际触摸触发,这并不是您真正想要的:如果您希望按下遥控器上的 Select 按钮来触发您的按钮,您应该使用 PrimaryActionTriggered。
let playPauseRecognizer = UITapGestureRecognizer(target: self, action: "playPauseRecognizer:")
playPauseRecognizer.allowedPressTypes = [NSNumber(integer:UIPressType.PlayPause.rawValue)]
view.addGestureRecognizer(playPauseRecognizer)
Swift @jess-bowers 代码的 3 版本:
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
let anyPress: UIPress? = presses.first
}
我已经按照此 tutorial 进行操作,一切正常。我遇到的唯一问题是我不知道如何检测按钮何时被按下?
提前致谢
按钮按下类似于 UITouch 事件。查看 UIResponder header 中的 UIPressEvent 回调。 UIViewControllers 是响应链的一部分,因此您可以在视图控制器中添加回调,类似于:
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event {
UIPress *anyPress = [presses anyObject];
// handle press event
}
根据 Apple Docs
,您可以像 iOS 一样向 UIButton 添加操作An instance of the UIButton class implements a button on the touch screen. A button intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action are inherited from UIControl. This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
此处教程的作者,向基于 TVML 的应用程序添加交互性的方法是在有问题的 DOM 元素上使用 addEventListener。您可以通过在创建期间保留对元素的引用或使用 getElementById 或其他类似的 JavaScript DOM 技术来找到 DOM 元素。另外,我应该提到我已经在提到的教程中添加了一个 "Part 2",其中包括这个作为它的主要焦点。
这是一个如何在 JS 中执行此操作的示例,假设我的DOMElement 是一个将您的按钮引用为 DOM 元素的变量。
myDOMElement.addEventListener("select", function() { alert("CLICK!") }, false);
当然,我有更多关于教程的信息,所以请随意 check that out。
您不应该在 tvOS 上使用 "UIControlEvents.TouchUpInside",因为这不会达到您的预期:您可能想改用 "UIControlEvents.PrimaryActionTriggered"。
TouchUpInside 由实际触摸触发,这并不是您真正想要的:如果您希望按下遥控器上的 Select 按钮来触发您的按钮,您应该使用 PrimaryActionTriggered。
let playPauseRecognizer = UITapGestureRecognizer(target: self, action: "playPauseRecognizer:")
playPauseRecognizer.allowedPressTypes = [NSNumber(integer:UIPressType.PlayPause.rawValue)]
view.addGestureRecognizer(playPauseRecognizer)
Swift @jess-bowers 代码的 3 版本:
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
let anyPress: UIPress? = presses.first
}