【问题标题】:TouchesMoved get label text only onceTouchesMoved 只获取一次标签文本
【发布时间】:2013-11-03 11:33:20
【问题描述】:

我正在开发一款经典游戏“找词” 像这个寻找“狮子”的例子

G H K J L D F G F Y E

J L M N L I O N A D T

G F O I A F E A D G H ...

我有这个代码来获取所有的字母值

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

[self touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];

CGPoint touchPoint = [[touches anyObject] locationInView:allLetters];
for (UILabel *letter in self.lettersArray) {


    if (touchPoint.x > letter.frame.origin.x &&
        touchPoint.x < letter.frame.origin.x + letter.frame.size.width &&
        touchPoint.y > letter.frame.origin.y &&
        touchPoint.y < letter.frame.origin.y + letter.frame.size.height )
    {

       [letter setBackgroundColor:[UIColor redColor]];
        NSLog(@"%@",letter.text);



    }


}}

它运行良好,因为当我用手指触摸标签文本时,它会正确获取它们。我的问题是我只需要获得一次价值......

现在它会多次获取该值,直到您传递给另一个字母

你有什么建议吗?

非常感谢!!

【问题讨论】:

    标签: ios uilabel touch-event


    【解决方案1】:

    为此,您必须保存最后一个字母边界,并检查您是否在同一个矩形中或已移开。

    首先,声明一个矩形来存储当前字母。

    CGRect lastLetter;

    然后按照以下方式修改您的方法:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self touchesBegan:touches withEvent:event];
    UITouch *touch = [[event allTouches] anyObject];
    
    CGPoint touchPoint = [[touches anyObject] locationInView:allLetters];
    for (UILabel *letter in self.lettersArray) {
    
    
        if (touchPoint.x > letter.frame.origin.x &&
            touchPoint.x < letter.frame.origin.x + letter.frame.size.width &&
            touchPoint.y > letter.frame.origin.y &&
            touchPoint.y < letter.frame.origin.y + letter.frame.size.height )
        {
    
           [letter setBackgroundColor:[UIColor redColor]];
            if(letter.frame.origin.x != lastLetter.origin.x)
            {
                 lastLetter = letter.frame;
                 NSLog(@"%@",letter.text);
            }
    
    
    
        }
    
    
    }}
    

    【讨论】:

    • 哇!谢谢老兄!太完美了……我也为 Y 添加了 if 语句
    【解决方案2】:

    您可以创建一个数组来存储所有触摸的标签,如下所示:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
       if(!_touchedLetters) // create the array if it doesn't exists
          _touchedLetters = [[NSMutableArray alloc] init];
    
       [self touchesBegan:touches withEvent:event];
       UITouch *touch = [[event allTouches] anyObject];
    
       CGPoint touchPoint = [[touches anyObject] locationInView:allLetters];
       for (UILabel *letter in self.lettersArray) {
    
        if([_touchedLetters containsObject:letter]) //continue if it already been added
            continue;
    
        if (touchPoint.x > letter.frame.origin.x &&
            touchPoint.x < letter.frame.origin.x + letter.frame.size.width &&
            touchPoint.y > letter.frame.origin.y &&
            touchPoint.y < letter.frame.origin.y + letter.frame.size.height )
        {
    
           [letter setBackgroundColor:[UIColor redColor]];
            NSLog(@"%@",letter.text);
    
           [_touchedLetters addObject:letter]
        }
    }}
    

    然后你可以清空数组 - (void)touchesEnded:withEvent:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多