0

我正在添加带有点击手势的子视图:

来自 UIView 类(masterButton):

[self addSubview:self.button];

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

视图控制器:

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

[self.view addSubview:button];

我删除子视图:

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

它触发播放音频文件的点击手势并且工作得很好,但是当我删除子视图并点击子视图所在的同一区域时,它会播放音频,就像子视图在那里一样。如果主视图没有响应子视图的任何手势,我该如何添加子视图?

我正在从 UIview 子类生成子视图,如果我尝试添加这样的手势:

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

它不起作用。你们中的任何人都知道为什么吗?

我会非常感谢你的帮助。

4

3 回答 3

2

添加按钮喜欢

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

[self.button removeFromSuperview];
于 2012-11-02T05:37:06.537 回答
0

您正在将 UITapGestureRecognizer 添加到主视图

将 UITapGestureRecognizer 添加到self.button而不是self

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];
[self addSubview:self.button];
于 2012-11-02T04:13:40.047 回答
0

只需将 Gesture 添加到 masterButton ,如下所示..

[self.button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(isTapped:)]];
于 2012-11-02T04:46:33.093 回答