Question

I want to do swipe gesture to slide the UIView continuously and getting data from it. Consider each word in each UIView. I stored data in an array and printed in label of UIView while transition. But when I try to swipe after shown all data program get stopped working. My project showing no errors. Please help me with it.

This is my array:

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];

This is my gesture recognizer coding:

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

There is a problem while increment only. I got NSLog print as FAIL. But I dont how to stop gesture recognizer if it reaches the value as [addArray count].

Was it helpful?

Solution

I would recommend incrementing or decrementing your index value (which you named increment) before the validity check, and if it is not valid reverse your action in an else. Like so:

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.
    }
}

And likewise for the other direction.

EDIT: To clarify, the original problem is that you check to make sure your index is valid, but, by incrementing after the check you end up making it invalid. Using your example, when the increment is 3 (the highest index of your array) it is in fact less than the array's count, which is 4. You then increment your index to 4, which would be out of bounds, or, in your case, fall into that if statement (which will no longer be needed using the suggestion) and log your FAIL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top