如何在目标c中使用用户点击手势获取标签标签
Posted
技术标签:
【中文标题】如何在目标c中使用用户点击手势获取标签标签【英文标题】:How to get tag of label with user tap gestures in objective c 【发布时间】:2017-05-09 09:47:10 【问题描述】:我怎样才能从标签循环中获取哪个标签正在被点击。我在一个循环中有 4 个标签,如果我点击标签为 3 的标签,我应该让另一个标签出现在已经隐藏的视图上,我怎样才能得到这个?
CGFloat y1 = 100.0;
CGFloat x1 = 30.0;
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < 4; i++)
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = 4+i;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label.text = @"Hello";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, y1, 50, 30)];
label2.tag = 100+i;
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor yellowColor];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label2.text = @"World";
label2.hidden = YES;
[self.view addSubview:label2];
rect1.origin.y += 45;
-(void)gotTapped:(UITapGestureRecognizer*)sender
for (UILabel *v in label2)
v.hidden = !v.hidden;
switch(((UITapGestureRecognizer *)sender).view.tag)
case 1:
NSLog(@"Clicked on label 1");
break;
case 2:
NSLog(@"Clicked on label 2");
break;
【问题讨论】:
【参考方案1】:更新答案。
你在你的代码中做了一些错误的坐标检查它的标签2:
y1=y1+45;// extra line u need to add
CGFloat y1 = 40;
CGFloat x1 = 30.0;
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < 4; i++)
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = i+1;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label.text = @"Hello";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(150, y1, 50, 30)];
label2.tag = i+100;
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor redColor];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:@"Verdana-Bold" size:8];
label2.text = [NSString stringWithFormat:@"%@%ld",@"world",(long)label2.tag];
label2.hidden = YES;
[self.view addSubview:label2];
rect1.origin.y += 45;
y1=y1+45;
NSLog(@"tag values of label2 are %ld",label2.tag);
// UITapGestureRecognizer 操作
-(void)gotTapped:(UITapGestureRecognizer*)sender
NSLog(@"%ld",(((UITapGestureRecognizer *)sender).view.tag));
int num= 99 +(int)(((UITapGestureRecognizer *)sender).view.tag);
NSLog(@"%d",num);
UILabel *label3 = [self.view viewWithTag:num];
label3.hidden = NO;
switch(((UITapGestureRecognizer *)sender).view.tag)
case 1:
NSLog(@"Clicked on label 1");
break;
case 2:
NSLog(@"Clicked on label 2");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 3:
NSLog(@"Clicked on label 3");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 4:
NSLog(@"Clicked on label 4");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
【讨论】:
是的,它可以很好地显示日志点击了哪个标签,但是如果我想在点击带有标签 3 的标签时获取我的隐藏标签,我该怎么做? 您能否更新您对我隐藏标签的答案,然后我会接受并为您投票,谢谢。 看到 label2 最初是隐藏的 @Alex。现在检查..我更新了答案,我认为它会满足你的要求 它提供所有标签触摸我希望隐藏标签仅在触摸标签具有标签 3 时才出现以上是关于如何在目标c中使用用户点击手势获取标签标签的主要内容,如果未能解决你的问题,请参考以下文章