我的 View 中有 5 个标签,分别标记为 1、2、3、4 和 5。我在它们上启用了用户交互,并添加了点击手势。
现在我想要的是获取被触摸标签的标签。
我正在做这样的事情:
tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureSelector)];
tapGesture.numberOfTapsRequired = 1.0;
- (void)tapGestureSelector :(id)sender
{
// I need the tag to perform different tasks.
// So that I would like to get the touched label's tag here.
}
如果我的问题不清楚,请问我。
感谢期待中的帮助。
最佳答案
首先,我添加了 oneLabel
和 twoLabel
作为 self.view
的 subview 。那我觉得就没有必要去获取tag了。
CGPoint tapPoint = [tapGesture locationInView:self.view];
if (CGRectContainsPoint(self.oneLabel.frame, tapPoint)) {
NSLog(@"tapped one label");
} else if (CGRectContainsPoint(self.twoLabel.frame, tapPoint)) {
NSLog(@"tapped two label");
}
关于objective-c - 点击时返回标签的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13304120/