如何将 UIGestureRecognizer 添加到 UIBarButtonItem 中,就像 iPad 应用程序上的常见撤消/重做 UIPopoverController 方案一样? [英] How can you add a UIGestureRecognizer to a UIBarButtonItem as in the common undo/redo UIPopoverController scheme on iPad apps?

查看:17
本文介绍了如何将 UIGestureRecognizer 添加到 UIBarButtonItem 中,就像 iPad 应用程序上的常见撤消/重做 UIPopoverController 方案一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在我的 iPad 应用程序中,我无法在按住事件后将弹出框附加到按钮栏项目.但这似乎是撤消/重做的标准.其他应用程序如何做到这一点?

In my iPad app, I cannot attach a popover to a button bar item only after press-and-hold events. But this seems to be standard for undo/redo. How do other apps do this?

背景

我的 UIKit (iPad) 应用程序的工具栏中有一个撤消按钮 (UIBarButtonSystemItemUndo).当我按下撤消按钮时,它会触发它的操作,即撤消:,并且正确执行.

I have an undo button (UIBarButtonSystemItemUndo) in the toolbar of my UIKit (iPad) app. When I press the undo button, it fires it's action which is undo:, and that executes correctly.

然而,iPad 上撤消/重做的标准 UE 约定"是按下撤消执行撤消,但按住按钮会显示一个弹出控制器,用户可以在其中选择撤消"或重做"直到控制器被解雇.

However, the "standard UE convention" for undo/redo on iPad is that pressing undo executes an undo but pressing and holding the button reveals a popover controller where the user selected either "undo" or "redo" until the controller is dismissed.

附加弹出框控制器的正常方法是使用presentPopoverFromBarButtonItem:,我可以很容易地配置它.为了让它仅在按住后显示,我们必须设置一个视图来响应长按"手势事件,如以下代码段所示:

The normal way to attach a popover controller is with presentPopoverFromBarButtonItem:, and I can configure this easily enough. To get this to show only after press-and-hold we have to set a view to respond to "long press" gesture events as in this snippet:

UILongPressGestureRecognizer *longPressOnUndoGesture = [[UILongPressGestureRecognizer alloc] 
       initWithTarget:self 
               action:@selector(handleLongPressOnUndoGesture:)];
//Broken because there is no customView in a UIBarButtonSystemItemUndo item
[self.undoButtonItem.customView addGestureRecognizer:longPressOnUndoGesture];
[longPressOnUndoGesture release];

这样,在视图上长按后,handleLongPressOnUndoGesture: 方法将被调用,在此方法中,我将配置和显示用于撤消/重做的弹出框.到目前为止,一切都很好.

With this, after a press-and-hold on the view the method handleLongPressOnUndoGesture: will get called, and within this method I will configure and display the popover for undo/redo. So far, so good.

问题在于没有视图可以附加.self.undoButtonItem 是一个 UIButtonBarItem,而不是一个视图.

The problem with this is that there is no view to attach to. self.undoButtonItem is a UIButtonBarItem, not a view.

可能的解决方案

1) [理想] 将手势识别器附加到按钮栏项.可以将手势识别器附加到视图,但 UIButtonBarItem 不是视图.它确实有 .customView 的属性,但是当 buttonbaritem 是标准系统类型时(在本例中是),该属性为零.

1) [The ideal] Attach the gesture recognizer to the button bar item. It is possible to attach a gesture recognizer to a view, but UIButtonBarItem is not a view. It does have a property for .customView, but that property is nil when the buttonbaritem is a standard system type (in this case it is).

2) 使用另一个视图.我可以使用 UIToolbar 但这需要一些奇怪的命中测试并且是一个全方位的黑客,如果可能的话.没有其他我能想到的替代视图.

2) Use another view. I could use the UIToolbar but that would require some weird hit-testing and be an all around hack, if even possible in the first place. There is no other alternative view to use that I can think of.

3) 使用 customView 属性.像 UIBarButtonSystemItemUndo 这样的标准类型没有 customView(它是零).设置 customView 将删除它需要的标准内容.这相当于重新实现 UIBarButtonSystemItemUndo 的所有外观和功能,如果可能的话,也是如此.

3) Use the customView property. Standard types like UIBarButtonSystemItemUndo have no customView (it is nil). Setting the customView will erase the standard contents which it needs to have. This would amount to re-implementing all the look and function of UIBarButtonSystemItemUndo, again if even possible to do.

问题

如何将手势识别器附加到这个按钮"上?更具体地说,我如何在 iPad 应用中实现标准的按住以显示重做弹出框?

How can I attach a gesture recognizer to this "button"? More specifically, how can I implement the standard press-and-hold-to-show-redo-popover in an iPad app?

想法?非常感谢,特别是如果有人真的在他们的应用程序中使用了这个功能(我在想你,omni)并且想要分享......

Ideas? Thank you very much, especially if someone actually has this working in their app (I'm thinking of you, omni) and wants to share...

推荐答案

注意:这不再适用于 iOS 11

代替尝试在工具栏的子视图列表中查找 UIBarButtonItem 的视图的麻烦,您也可以尝试此操作,一旦将项目添加到工具栏:

In lieu of that mess with trying to find the UIBarButtonItem's view in the toolbar's subview list, you can also try this, once the item is added to the toolbar:

[barButtonItem valueForKey:@"view"];

这使用键值编码框架来访问 UIBarButtonItem 的私有 _view 变量,它保存它创建的视图.

This uses the Key-Value Coding framework to access the UIBarButtonItem's private _view variable, where it keeps the view it created.

当然,我不知道这在 Apple 的私有 API 方面属于什么地方(这是用于访问公共类的私有变量的公共方法 - 不像访问私有框架来制作花哨的 Apple 专用效果或任何东西),但它确实有效,而且相当轻松.

Granted, I don't know where this falls in terms of Apple's private API thing (this is public method used to access a private variable of a public class - not like accessing private frameworks to make fancy Apple-only effects or anything), but it does work, and rather painlessly.

这篇关于如何将 UIGestureRecognizer 添加到 UIBarButtonItem 中,就像 iPad 应用程序上的常见撤消/重做 UIPopoverController 方案一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆