I am kinda new to iPhone development and haven't done anything yet envolving touches. 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.
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
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. Then you'll have to use the delegate to message your view's controller so it can hide the navigationBar
.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps == 1)
{
[delegateController tapDidOccur];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With