Можно ли подключить UITapGestureRecognizer к подклассу UILabel
Я пытаюсь подключить распознаватель жестов к своему классу, который является подклассом UILabel, но он не работает. Можете ли вы помочь мне понять, что не так в коде
@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
Ответы
Ответ 1
Ваш код должен работать нормально, единственное, что вам может понадобиться исправить, - это то, что пользовательское взаимодействие отключено для UILabel по умолчанию, поэтому распознаватель жестов не получает никаких событий касания. Попробуйте вручную включить его, добавив эту строку в свой код (например, в методе init):
self.userInteractionEnabled = YES;
Ответ 2
Да, это возможно, Любой класс наследуемый из UIView
.
Не забудьте включить взаимодействие с пользователем.
self.userInteractionEnabled = YES;
Ответ 3
Вы можете использовать нижеприведенный код, чтобы добавить жест нажатия на UILable: -
Шаг 1:
Delegate "UIGestureRecognizerDelegate" to your viewcontroller.h
for example:
@interface User_mail_List : UIViewController<UIGestureRecognizerDelegate>
Шаг 2:
//create you UILable
UILabel *title_lbl= [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[title_lbl setText:@"u&me"];
[title_lbl setUserInteractionEnabled:YES];
[yourView addSubview:title_lbl];
Шаг 3:
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Prof_lbl_Pressed:)];//your action selector
[tap setNumberOfTapsRequired:1];
title_lbl.userInteractionEnabled= YES;
[title_lbl addGestureRecognizer:tap];
Шаг 4:
-(void)Prof_lbl_Pressed:(id)sender{
//write your code action
}
спасибо,