【问题标题】:is it possible to add a GestureRecognizer to a label in a custom UIView subview是否可以将 GestureRecognizer 添加到自定义 UIView 子视图中的标签
【发布时间】:2014-03-10 21:54:21
【问题描述】:

我正在重构我采用的ViewController,并在控制器代码中创建UIViews。我想将其移至自定义 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 中子视图属性的标签中?有什么明显的我做错了吗?

【问题讨论】:

    标签: ios uiview uilabel uigesturerecognizer


    【解决方案1】:

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

    【讨论】:

    • thx Wain,我需要在控制器或自定义视图中设置它吗?还是没关系?
    • 我会在创建标签时将其设置在视图中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多