Remove overlay view on touch
I defined a UIview as an overlay view:
self.disableViewOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0f,44.0f,320.0f,416.0f)];
self.disableViewOverlay.backgroundColor=[UIColor blackColor];
self.disableViewOverlay.alpha = 0;
Now I would like to disable the overlay view if a user taps o开发者_开发百科n it...
[disableViewOverlay removeFromSuperview];
How do I find out if the user taps on this overlay view? Is there a "tapped" method just like a button has an IBAction?
Thanks!
You have two options:
overriding
hitTest
in yourUIView
;overriding
touchesBegan:withEvent:
,touchesMoved:withEvent:
,touchesEnded:withEvent:
for greater control.
Both approaches require that you subclass your UIView
(the one you want to detect the tap on).
Have a look at UIView Class Reference or UIResponder Class Reference respectively.
If you do not want to subclass your UIView
, another option is attaching it to a UITapGestureRecognizer
:
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnView)];
[self.disableViewOverlay addGestureRecognizer:gestureRecognizer];
you would then disable the overlay in the handleTapOnView
function.
Gesture recognizers are available only from iOS 3.2 on.
Check this: UITapGestureRecognizer Class Reference
精彩评论