Swipe Left and Right to load item

119 Views Asked by At

i had an array which is contains 10 items which is 1,2,3,4,5,6,7,8,9,10. when i load the page the item is at 3. so when the user swipe left it will go to 4 and swipe to right it will go to 2. may i know how to do it?

newsID=[[NSMutableArray alloc]init];
[newsID addObject:@"1"];
[newsID addObject:@"2"];
[newsID addObject:@"3"];
[newsID addObject:@"4"];
[newsID addObject:@"5"];
[newsID addObject:@"6"];
[newsID addObject:@"7"];
[newsID addObject:@"8"];
[newsID addObject:@"9"];
[newsID addObject:@"10"];
UISwipeGestureRecognizer * swipeleft=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
2

There are 2 best solutions below

0
On

Try this. i am taking one UILabel and changing the text of it while swiping.and one int variable to keep track of swipe.

- (void)viewDidLoad 
{
   [super viewDidLoad];
   newIdArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
   num_index = 3;
   ValueLabel.text = [newIdArray objectAtIndex:num_index];
   leftGesture = [[UISwipeGestureRecognizer alloc]init];
   leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
   [leftGesture addTarget:self action:@selector(PerformSwipegesture:)];
   [self.view addGestureRecognizer:leftGesture];
   rightGesture = [[UISwipeGestureRecognizer alloc]init];
   rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
   [rightGesture addTarget:self action:@selector(PerformSwipegesture:)];
   [self.view addGestureRecognizer:rightGesture]; 
} 


 - (IBAction)PerformSwipegesture:(UISwipeGestureRecognizer *)sender
 {
     if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
     {
       if (num_index <9)
       {
         num_index = num_index +1;
         ValueLabel.text = [newIdArray objectAtIndex:num_index];
       }
     }
     else if (sender.direction == UISwipeGestureRecognizerDirectionRight)
     {
         if (num_index >0) 
         {
            if (num_index >1)
              num_index = num_index -2;
            else
              num_index = num_index -1;
            ValueLabel.text = [newIdArray objectAtIndex:num_index];
         }
     }
}
0
On

Here is simple example for handling swipe gestures. Here i am taking one UILabel and changing the text of it while swiping.

- (void)viewDidLoad 
  {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    _lblText.text=@"3";
    _lblText.textAlignment=NSTextAlignmentCenter;


    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];


    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

}

-(void)handleRightSwipe
{

    if ([_lblText.text intValue]>1)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]-1];
    }
    else
    {
        return;
    }
}

-(void)handleLeftSwipe
{

    if ([_lblText.text intValue]<10)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]+1];
    }
    else
    {
        return;
    }

}