2

我想做滑动手势来UIView连续滑动并从中获取数据。考虑每个UIView. 我将数据存储在一个数组中并打印在UIViewwhile 转换的标签中。但是当我在显示所有数据程序停止工作后尝试滑动时。我的项目没有显示错误。请帮帮我。

这是我的数组:

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打印失败。但是如果手势识别器达到[addArray count].

4

1 回答 1

3

我建议在有效性检查之前增加或减少您的索引值(您将其命名为增量),如果它无效,则在 else 中反转您的操作。像这样:

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,这将超出范围,或者在您的在这种情况下,属于 if 语句(使用该建议将不再需要该语句)并记录您的 FAIL。

于 2012-01-10T18:45:12.337 回答