UILabel 文本从触摸开始事件



我可以通过什么方法使用 touchesStarted 事件从 UILabel 中发现文本?

到目前为止,当前的代码是...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
     UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
     ////
     //insert code here to get currentLabel - not using viewYouWishToObtain.tag
     ////
     NSLog(@"text: %@", currentLabel.text);
     }

我希望我能简单地写这些...但出于不同的狡猾原因,这些都是邪恶的

UILabel* currentLabel = [self.view hitTest:locationPoint withEvent:event];
NSLog(@"text: %@", currentLabel.text);
NSLog(@"text: %@",[self.view hitTest:locationPoint withEvent:event].text);
UILabel *newlabel = (UILabel*) event;
NSLog(@"text: %@", newlabel.text);

这几乎是功能等效的代码,但一般功能现在依赖于 NSDictionary。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray* myword=[NSArray arrayWithObjects:@"h",@"e",@"l",@"l",@"o",nil];
    NSDictionary *letterToNumber;
    letterToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
                @"0", @"a", 
                @"1", @"b", 
                @"2", @"c", 
                @"3", @"d",
                @"4", @"e", 
                @"5", @"f", 
                @"6", @"g", 
                @"7", @"h", 
                @"8", @"i", 
                @"9", @"j", 
                @"10", @"k", 
                @"11", @"l", 
                @"12", @"m", 
                @"13", @"n", 
                @"14", @"o", 
                @"15", @"p", 
                @"16", @"q", 
                @"17", @"r", 
                @"18", @"s", 
                @"19", @"t", 
                @"20", @"u", 
                @"21", @"v", 
                @"22", @"w", 
                @"23", @"x", 
                @"24", @"y", 
                @"25", @"z", 
                nil];    
    NSUInteger characterCount = [myword count];
    for (int i=0;i<characterCount ;i++){
    UILabel*myLabel;
    myLabel=[[UILabel alloc] initWithFrame: CGRectMake((1+i)*35.0, 100.0, 30.0, 30.0)];
    myLabel.backgroundColor = [UIColor whiteColor];
    myLabel.text= [myword  objectAtIndex:i];
    myLabel.userInteractionEnabled = YES;
    myLabel.tag=100+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];        
    [self.view addSubview:myLabel];     
    }
 }
   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      NSLog(@"1");
      CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
      UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
      NSArray*myarray;
      myarray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];
      NSUInteger temp = viewYouWishToObtain.tag;
      if (temp >= 100){
      NSLog(@"text: %@",[myarray  objectAtIndex:temp-100]);
      }
      if ([touch view] != viewYouWishToObtain && (viewYouWishToObtain.tag >= 100)) {
          if ([touch tapCount] == 2) {
          }
          return;
      }
  }

帮助解决此编码问题会很棒

只是出于好奇,你能不能从使用 UILabel 切换到只使用 UIButton 和自定义类型。此时它应该看起来像一个UILabel,你可以正常连接触摸事件,而不需要命中测试。我认为这会让你的事情变得容易得多。

UILabel * label = [[UILabel alloc] init];
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
[label addGestureRecognizer:tapGestureRecognizer];

然后使用 labelTap 函数追溯标签并获取文本;)

最新更新