[英]iOS main view is responding to subview gestures
I'm adding subviews with tap gestures: 我要添加带有点击手势的子视图:
from UIView class(masterButton): 从UIView类(masterButton):
[self addSubview:self.button]; // Add gesture recognizers [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];
ViewController: ViewController:
masterButton *button = [[masterButton alloc] initWithFrontImage:img ]; masterButton * button = [[[masterButton alloc] initWithFrontImage:img];
[self.view addSubview:button]; [self.view addSubview:button];
I removing the subview: 我删除子视图:
UIView * button= [controller.view viewWithTag:controller.tagButton]; UIView * button = [controller.view viewWithTag:controller.tagButton]; [button removeFromSuperview]; [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: 我从UIview子类生成子视图,如果我尝试添加如下手势:
[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]]; [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 您正在将UITapGestureRecognizer添加到主视图
Add UITapGestureRecognizer to self.button
instead of self
将UITapGestureRecognizer添加到self.button
而不是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:)]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.