是否可以将UITapGestureRecognizer附加到UILabel子类 [英] Is it possible to attach UITapGestureRecognizer to UILabel subclass

查看:122
本文介绍了是否可以将UITapGestureRecognizer附加到UILabel子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将手势识别器附加到我自己的类,这是UILabel的子类,但它不起作用。你能帮我理解代码中的错误吗?

I'm trying to attach gesture recognizer to my own class which is subclass of UILabel, but it does not work. Can you help me to understand what's wrong in the code

 
@interface Card : UILabel  {

}

- (void) addBackSideWord;

@end

#import "Card.h"

@implementation Card
- (id)initWithFrame:(CGRect)frame {

    if ((self = [super initWithFrame:frame])) {

        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                         initWithTarget:self action:@selector(addBackSideWord)];
        [tapRecognizer setNumberOfTouchesRequired:2];
        [tapRecognizer setDelegate:self];
        [self addGestureRecognizer:tapRecognizer];
    }

    return self;
}

- (void) addBackSideWord {

     //do something
}
@end


推荐答案

您的代码应该可以正常工作,您可能需要解决的唯一问题是默认情况下,UILabel禁用了用户交互,因此手势识别器不会收到任何触摸事件。尝试通过在代码中添加此行来手动启用它(例如在init方法中):

Your code should work fine, the only thing you may need to fix is that user interaction is disabled for UILabel by default, so gesture recogniser does not receive any touch events. Try manually enable it by adding this line to your code (e.g. in init method):

self.userInteractionEnabled = YES;

这篇关于是否可以将UITapGestureRecognizer附加到UILabel子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆