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

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

问题描述

问题

在我的iPad应用程式中,我无法在以下位置附加弹出式视窗到按住事件。但是这似乎是undo / redo的标准。其他应用程序如何做到这一点?

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?

背景

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

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.

附加popover控制器的常用方法是使用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是一个标准的系统类型(在这种情况下)时,该属性为nil。

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(它是nil)。设置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...

推荐答案

在工具栏的子视图列表中尝试查找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"];

这使用Key-Value Coding框架来访问UIBarButtonItem的私有_view变量,它创建。

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

授予,我不知道这在苹果的私人API的东西是什么(这是公共方法用于访问一个公共类的私有变量 - 不像访问私有框架以产生只有苹果的效果或任何东西),但它可以工作,而且无痛。

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像在常见的撤消/重做UIPopoverController方案在iPad应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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