UITapGestureRecognizer在触摸时不响应

时间:2016-11-01 06:55:06

标签: ios objective-c xcode uilabel

我正在创建一个带有属性字符串的UILabel并将其分配给导航栏标题。它显示在我想要的地方。我还想附加它UITapGestureRecognizer但我无法在识别器中调用该函数。下面你可以看到我正在尝试的代码。

label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.numberOfLines = 4;

// set different font for title and subtitle
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"\n Title \n \n"];
//[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:1.0] range:NSMakeRange(0,string.length)];

// set line spacing
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:-10];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];

label.font = [UIFont fontWithName:@"Avenir-Medium" size:22.0f]; 
self.navigationItem.titleView = label;
label.attributedText = string;
[label sizeToFit];

UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(clickTitleLabel)];
[label addGestureRecognizer:tapGesture];

2 个答案:

答案 0 :(得分:0)

<强>步骤1

最初检查一次setUserInteractionEnabled:YES或不

[label setUserInteractionEnabled:YES];

<强>步骤-2

else check once frame and clickable area 

<强>步骤-3

添加所需的点击次数

tapGesture.numberOfTapsRequired = 1;

答案 1 :(得分:0)

将框架重置为CGRectZero标签