views:

1598

answers:

1

Hello,

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!

+1  A: 

You can use the method touchesBegan in UIView to detect the tap, so you will have to subclass have a custom subclass of UIView for the viewcontrollers view that you would like to detect taps on. Then youll have to use then delegate the message to your views controller so it can hide the navigationBar.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger numTaps = [[touches anyObject] tapCount];
    if (numTaps == 1)
    {
           [delegateController tapDidOccur];  
        }
}
Daniel
This does work. Thank you. Now I'm having a bit of issue assing that event to the delegate itsef. Do I have to extent the UIScrollViewDelegate interface to declare a tapDidOccur method?
leolobato
I overrode the delegate @property on my subclass of UIScrollView to implement a "tappable" protocol with my "tapDidOccur" method and it worked fine now. Thanks!
leolobato