滑动手势以滑动UIViews

时间:2012-01-10 18:17:55

标签: ios arrays uiview increment uiswipegesturerecognizer

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

这是我的阵列:

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]的值,我就不会停止手势识别器。

1 个答案:

答案 0 :(得分:3)

我建议在有效性检查之前递增或递减您的索引值(您将其命名为increment),如果它无效则在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语句(使用该建议将不再需要)并记录你的失败。