无法将手势识别器应用于导航栏 [英] Trouble applying gesture recognizer to navigationbar
问题描述
在我的iPad应用程序中,我在屏幕上有多个视图.
In my iPad Application I have multiple views on a screen.
我想做的是在导航栏上双击手势识别器.但是我没有成功,但是当将相同的手势识别器应用于该视图时,它就可以工作.
What I want to do is apply a double tap Gesture Recognizer to the Navigation Bar. But I had no success, however when the same gesture recognizer applied to that view it works.
这是我正在使用的代码:
Here is the code I am using:
// Create gesture recognizer, notice the selector method
UITapGestureRecognizer *oneFingerTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:oneFingerTwoTaps];
这可以在视图上使用,但是完成后:
This works on view, but when this is done:
[self.navigationController.navigationBar addGestureRecognizer:oneFingerTwoTaps]
不起作用.
推荐答案
为此,您需要继承UINavigationBar的子类,重写其中的init按钮,然后在其中添加手势识别器.
For this you need to subclass UINavigationBar, override the init button in it and add your gesture recognizer there.
因此,假设您创建了一个名为'CustomNavigationBar'的子类-在您的m文件中,您将具有如下所示的init方法:
So say you make a subclass called 'CustomNavigationBar' - in your m file you would have an init method like this:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
UISwipeGestureRecognizer *swipeRight;
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeRight setNumberOfTouchesRequired:1];
[swipeRight setEnabled:YES];
[self addGestureRecognizer:swipeRight];
}
return self;
}
然后,您需要在界面构建器中将导航栏的类名称设置为子类的名称.
Then you need to set the class name of your navigation bar in interface builder to the name of your subclass.
将代理协议添加到导航栏也很方便,以侦听手势结尾处发送的方法.例如-在上述向右滑动的情况下:
Also it's handy to add a delegate protocol to your navigation bar to listen for methods sent at the end of your gestures. For example - in the case of the above swipe right:
@protocol CustomNavigationbarDelegate <NSObject>
- (void)customNavBarDidFinishSwipeRight;
@end
然后在m文件中-在手势识别方法(无论您采用哪种方法)上,您都可以触发此委托方法.
then in the m file - on the gesture recognised method (whatever you make it) you can trigger this delegate method.
希望这会有所帮助
这篇关于无法将手势识别器应用于导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!