0

touchesBeganイベントを使用して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;
      }
  }

このコーディングの問題を解決するのは素晴らしいことです

4

2 に答える 2

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

次に、labelTapped 関数を使用してラベルをトレース バックし、テキストを取得します ;)

于 2012-05-23T09:29:06.580 に答える
0

好奇心から、UILabel の使用からカスタム タイプの UIButton の使用に切り替えることはできますか。その時点でUILabelのように見えるはずで、ヒットテストを必要とする代わりに、通常どおりタッチイベントを接続できます。私はそれがあなたにとって物事をずっと簡単にするだろうと思います.

于 2012-05-17T20:39:07.583 に答える