2013-11-03 7 views
-1
내가 고전 게임에 일하고 있어요

"사자"를 찾고이 예와 같이 "단어를 찾을 수 있습니다"레이블 텍스트를 얻을 수있는 touchesMoved한 번만

GHKJLDFGFYE

JLMN LION ADT

GFOIAFEADGH

- (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); 



    } 


}} 

손가락으로 터치하면 레이블 텍스트가 올바르게 표시되기 때문에 제대로 작동합니다. 당신이 어떤 조언을해야합니까 다른 편지

에 전달 될 때까지 내 문제는

지금은 값을 가져옵니다 ... 내가 한 번만 가치를 얻을 필요가 많은 몇 번입니까?

정말 고마워요 !!

답변

1

이렇게하려면 마지막 문자 범위를 저장하고 동일한 rect에 있는지 또는 멀리 옮겨야하는지 확인해야합니다.

먼저 현재 문자를 저장할 rect를 선언하십시오.

CGRect lastLetter;

그런 다음 아래에 따라 당신의 방법을 수정

- (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)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); 
     } 



    } 


}} 
+0

와우! 고마워 친구! 그건 완벽합니다 ... 나는 if 문을 Y에 추가합니다. –

0

당신은이 같은 모든 레이블을 감동 저장 배열, 뭔가를 만들 수 있습니다 배열 - (void) touchesEnded : withEvent :