简体   繁体   English

在 UIBarButtonItem 中检测触摸或按下

[英]Detecting touch or press in UIBarButtonItem

I have a ScrollView which shows photos.我有一个显示照片的 ScrollView。 When touching the screen, a UIToolBar pops up, with several UIBarButtonItem buttons, like Previous, Next, Play... If the users doesn't do anything for 5 seconds, the toolbar disappears again.当触摸屏幕时,会弹出一个 UIToolBar,有几个 UIBarButtonItem 按钮,如上一个、下一个、播放……如果用户 5 秒没有做任何事情,工具栏再次消失。

It's all very similar to the Apple Photos app.这一切都与 Apple 照片应用程序非常相似。

Everything works as it should, but there's one thing I'm struggling with: I cannot get touches for if the user pressed the buttons, nor can I check on the highlight status of the button.一切正常,但有一件事我正在努力:如果用户按下按钮,我无法获得触摸,也无法检查按钮的突出显示状态。

So there's a problem if the user keeps a button pressed for a few seconds... the program will assume nothing has happened, and remove the toolbar after the 5 seconds have passed.因此,如果用户按住按钮几秒钟,就会出现问题……程序将假定什么都没发生,并在 5 秒钟过去后移除工具栏。

The Photos app does not have this problem.照片应用没有这个问题。 Even better: when you keep the Next or Previous button pressed or longer than a second, it already executes the "action" associated with the UIBarButtonItem !更好的是:当您按住 Next 或 Previous 按钮或超过一秒钟时,它已经执行了与 UIBarButtonItem 关联的“操作”!

In short, I want to do one of these: - to know whether a UIBarButtonItem is pressed - to know whether a UIBarButtonItem is highlighted - or else just know whether there's any press anywhere going on简而言之,我想执行以下操作之一: - 了解是否按下了 UIBarButtonItem - 了解 UIBarButtonItem 是否突出显示 - 或者只知道是否有任何按下

You could try attaching a custom subclass of UIGestureRecognizer , which tells you when the users interacts with the view to the toolbar.您可以尝试附加UIGestureRecognizer的自定义子类,它会告诉您用户何时与工具栏的视图交互。

Alternatively, you could try subclassing UIToolbar and change its -[touchesBegan:withEvent:] and -[touchesEnded:withEvent:] methods to tell you when the users starts/stops interacting with anything on the toolbar.或者,您可以尝试UIToolbar并更改其-[touchesBegan:withEvent:]-[touchesEnded:withEvent:]方法,以告诉您用户何时开始/停止与工具栏上的任何内容交互。

The latter is probably simpler.后者可能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM