Juan
Juan

Reputation: 637

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.

Upvotes: 0

Views: 996

Answers (3)

Laxus Laxus
Laxus Laxus

Reputation: 36

Add button like

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

[self.button removeFromSuperview];

Upvotes: 2

Paras Joshi
Paras Joshi

Reputation: 20551

just add Gesture to the masterButton like bellow..

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

Upvotes: 0

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

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];

Upvotes: 0

Related Questions