简体   繁体   English

检测点击以显示/隐藏UINavigationBar

[英]Detecting tap to Show/hide UINavigationBar

I am kinda new to iPhone development and haven't done anything yet envolving touches. 我是iPhone开发的新手,并没有做任何有趣的事情。 My view hierarchy like this: 我的视图层次结构如下:

 UIView - UIImageView - UIScrollView - CustomView 

How do I detect if the user has tapped anywhere on the screen so I can show/hide the navigation bar accordingly? 如何检测用户是否点击了屏幕上的任何位置,以便我可以相应地显示/隐藏导航栏? I don't need user interaction on my CustomView but I'd like to ignore touches on the UIScrollView when the user just wants to drag it. 我不需要在我的CustomView上进行用户交互,但是当用户只想拖动它时,我想忽略UIScrollView上的触摸。

I can already show/hide the navigation bar from my view controller programatically using: 我已经可以使用以下命令以编程方式显示/隐藏我的视图控制器中的导航栏:

[self.navigationController setNavigationBarHidden:YES animated:YES];

Thanks in advance! 提前致谢!

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHideNavbar:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

-(void) showHideNavbar:(id) sender { // write code to show/hide nav bar here }

This is the way to do it using UIGestureRecognizers available on iOS4 这是使用iOS4上提供的UIGestureRecognizers进行此操作的方法

You can use the method touchesBegan in UIView to detect the tap, so you will have to have a custom subclass of UIView for the viewcontroller's view that you would like to detect taps on. 您可以使用UIView中的touchesBegan方法来检测tap,因此您必须为viewcontroller的视图设置一个UIView的自定义子类,以便您检测点击。 Then you'll have to use the delegate to message your view's controller so it can hide the navigationBar . 然后,您将必须使用委托给视图的控制器发送消息,以便它可以隐藏navigationBar

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger numTaps = [[touches anyObject] tapCount];
    if (numTaps == 1)
    {
        [delegateController tapDidOccur];  
    }
}

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

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