2012-01-10 238 views
2

我想做滑動手勢,連續滑動UIView並從中獲取數據。考慮每個UIView中的每個單詞。我將數據存儲在數組中,並在轉換時以UIView的標籤打印。但是當我顯示所有數據程序停止工作後,試圖刷卡時。我的項目顯示沒有錯誤。請幫助我。滑動手勢滑動UIViews

這是我的數組:

addArray = [[NSMutableArray alloc]initWithCapacity:4]; 
[addArray insertObject:@"10" atIndex:0]; 
[addArray insertObject:@"20" atIndex:1]; 
[addArray insertObject:@"30" atIndex:2]; 
[addArray insertObject:@"40" atIndex:3]; 

flippedArray = [[NSMutableArray alloc] initWithCapacity:4]; 
[flippedArray insertObject:@"100" atIndex:0]; 
[flippedArray insertObject:@"200" atIndex:1]; 
[flippedArray insertObject:@"300" atIndex:2]; 
[flippedArray insertObject:@"400" atIndex:3]; 

這是我的手勢識別編碼:

-(void)swipegesture:(UISwipeGestureRecognizer *)recognizer{ 

    CGPoint location = [recognizer locationInView:additionalView]; 
    if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
    { 
     if (increment<[addArray count]) 
     { 
      NSLog(@"%d",[addArray count]); 
      increment++; 
      if(increment==[addArray count]) 
      { 
       NSLog(@"Fail"); 
       //[recognizer requireGestureRecognizerToFail:swipeGesture]; 
       [recognizer setEnabled:NO]; 
      } 
      else 
      { 
       additionalLabel.text=[[NSString alloc] initWithFormat:@"%@", 
             [addArray objectAtIndex:increment]]; 
       flippedLabel.text = [[NSString alloc] initWithFormat:@"%@", 
             [flippedArray objectAtIndex:increment]]; 
       NSLog(@"increment %d",increment); 
       [UIView animateWithDuration:0.55 animations:^{ 
        [UIView setAnimationDelay:0.2]; 
       }]; 
       CATransition *animation = [CATransition animation]; 
       [animation setType:kCATransitionPush]; 
       [animation setSubtype:kCATransitionFromRight]; 
       [animation setTimingFunction:[CAMediaTimingFunction 
           functionWithName:kCAMediaTimingFunctionDefault]]; 
       [animation setSpeed:0.4]; 
       [[additionalView layer] addAnimation:animation forKey:nil]; 
      } 
     } 
    } 
    else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) 
    { 
     if (increment>=0 && increment<[addArray count]) 
     { 
      increment--; 
      if(increment>[addArray count]) 
      { 
       additionalLabel.text=[[NSString alloc]initWithFormat:@"%@", 
             [addArray objectAtIndex:increment]]; 
       flippedLabel.text=[[NSString alloc]initWithFormat:@"%@", 
             [flippedArray objectAtIndex:increment]]; 
       NSLog(@"Decrement %d",increment); 
       [UIView animateWithDuration:0.55 animations:^{ 
        [UIView setAnimationDelay:0.2]; 
       }]; 
       CATransition *animation = [CATransition animation]; 
       [animation setType:kCATransitionPush]; 
       [animation setSubtype:kCATransitionFromLeft]; 
       [animation setTimingFunction:[CAMediaTimingFunction 
           functionWithName:kCAMediaTimingFunctionDefault]]; 
       [animation setSpeed:0.4]; 
       [[additionalView layer] addAnimation:animation forKey:nil]; 
      } 
     } 
    } 
} 

這裏有一個問題,而僅增加。我將NSLog打印爲FAIL。但是,如果它達到[addArray count]的值,我不會如何停止手勢識別器。

回答

3

我會建議在有效性檢查之前遞增或遞減您的索引值(您命名爲increment),並且如果它不是有效的,則在其他操作中反轉您的操作。像這樣:

if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) 
{ 
    increment++; 

    if (increment<[addArray count]) 
    { 
     // Your code 
    } 
    else 
    { 
     increment--; // The increment would pass the range of the array, set it back. 
    } 
} 

而且對於其他方向也是如此。

編輯:澄清,原來的問題是,你檢查,以確保您的索引是有效的,但通過檢查後增加最終使其無效。使用你的例子,當增量爲3(數組的最高索引)時,它實際上小於數組的數量,即4。然後,將索引增加到4,這會超出範圍,或者在的情況下,將陷入如果陳述(這將不再需要使用建議)並記錄你的失敗。

+1

正好。如果(增量<[addArray計數])檢查應該是如果(增量+ 1 <[addArray計數]) – Lefteris 2012-01-10 18:53:28

+0

謝謝你這麼多的夥計...真的,我很高興看到你的答案... – 2012-01-11 05:59:37

+0

它的工作完美.. 。 – 2012-01-11 06:00:03