是否可以将GestureRecognizer添加到自定义UIView子视图中的标签

时间:2014-03-10 21:54:22

标签: ios uiview uilabel uigesturerecognizer

我正在重构我已采用的ViewController并在控制器代码中创建UIView。我想将其移至自定义UIView,但想要在控制器中处理手势识别器。

我有ItemViewController,其中包含自定义UIView。在ItemController中,我想做类似的事情:

ItemView *itemView = [[ItemView alloc] initWithFrame:CGRectMake(30.0f,_runningYPosition,100.0f, 50.0f)];
UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
itemView.miHeaderLabel.tag = [item itemID];
[itemView.miHeaderLabel addGestureRecognizer: tapRecognizer2];

引用

- (void)tapRecognized:(id)sender
{
    ...
}
ItemViewController中的

(不在自定义视图中)。

但它似乎不起作用。是否可以将GestureRecognizer添加到标签中,该标签是ViewController中子视图的属性?有什么明显的我做错了吗?

1 个答案:

答案 0 :(得分:1)

您需要在标签上将userInteractionEnabled设置为YES才能接受触摸(因此手势接收触摸)。