Question

I'm currently creating a drop-down search bar inside my app. When the drop down animation begins a new -UIView overlays the current one to create the black transparent effect.

However I need to know how I can create a listener that sends an action once my -UIView is touched.

This is how I created the transparent -UIView / -UIWindow

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    blackView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 750)];
    blackView.backgroundColor = [UIColor blackColor];
    [window addSubview:blackView];
    blackView.alpha = 0.6;

Image of the drop-down and the -UIView.

App Image

Was it helpful?

Solution

Best way is Add UITapGestureRecognizer to your view such like,

UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TouchViewMethod:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[self.yourViewName addGestureRecognizer:gestureRec];

Method

-(void)TouchViewMethod:(UITapGestureRecognizer *)touch
{

   //// Do your stuff;

}

OTHER TIPS

I think you have to implement

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
or you have to add the tapgetsture recognizer to the view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top