简体   繁体   中英

iOS main view is responding to subview gestures

I'm adding subviews with tap gestures:

from UIView class(masterButton):

 [self addSubview:self.button]; // Add gesture recognizers [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]]; 

ViewController:

masterButton *button = [[masterButton alloc] initWithFrontImage:img ];

[self.view addSubview:button];

I removing the subview:

UIView * button= [controller.view viewWithTag:controller.tagButton]; [button removeFromSuperview];

The tap gesture it triggers to play audio file and works just fine but when I remove the subview and I tap the same area where the subview was it plays the audio like if the subview was there. How can I add the subview in a way were the main view is not responding to any gesture of the subviews?

I'm generating the subviews from UIview subclass and if I try to add the gesture like this:

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];

it doesn't work. any of you knows why?

I'll really appreciate your help.

Add button like

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]]

[self.button removeFromSuperview];

You are adding UITapGestureRecognizer to the mainview

Add UITapGestureRecognizer to self.button instead of self

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];
[self addSubview:self.button];

只需将手势添加到masterButton即可,例如下面。

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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