Search code examples
iosuiscrollviewuiimageviewuiswipegesturerecognizer

How to show NextImage in ImageView when User SwipeLeft in ImageView through UISwipeGestureRecognizer in iOS?


i am Newbie in iOS Development. i want to make an application in my application i make one UIScrollview and in this UIScrollView i add an UIImageview. when viewDidAppear i set imageview image like as

-(void)viewDidAppear:(BOOL)animated
{
NSDictionary *dict=[self.imagesa objectAtIndex:0];
NSString *imagelink=[dict valueForKey:@"link"];
NSLog(@"imageLink %@",imagelink);
[self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:[UIImage imageNamed:@"1.png"]];
}

And a code to add a Gesture for my Imageview and Scrollvie like as

 UISwipeGestureRecognizer *swipeGestureLeftdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
swipeGestureLeftdirection.delegate=self;
swipeGestureLeftdirection.direction=UISwipeGestureRecognizerDirectionLeft;
[self.bigImage addGestureRecognizer:swipeGestureLeftdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureLeftdirection];

 UISwipeGestureRecognizer *swipeGestureRightdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToRighttWithGestureRecognizer:)];
swipeGestureRightdirection.delegate=self;
swipeGestureRightdirection.direction=UISwipeGestureRecognizerDirectionRight;
[self.bigImage addGestureRecognizer:swipeGestureRightdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureRightdirection];

Here Self.bigimage is my UIImageview and self.bigScrollview is my UIScrollView. Now i want when User Swipe left then i want UIImageview image as self.imagesa next index and when swipe right then i want UIImageVIew image back index image how it possible. Please give me Solution for it.


Solution

  • do no use loop inside, slideToLeftWithGestureRecognizer:

    write code like this

     -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
    { 
         if(index<[self.imagesa count])
         {
             NSDictionary *dict=[self.imagesa objectAtIndex:index];
            NSString *imagelink=[dict valueForKey:@"link"];
            NSLog(@"imageLink %@",imagelink);
           [self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:        [UIImage imageNamed:@"1.png"]];
    
        index++;
       }
     }
    

    make index as global vairable, on viewDidLoad initailize with 0.

      -(void)slideToRighttWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
       { 
          if(index>0)
         {
            index--;
            NSDictionary *dict=[self.imagesa objectAtIndex:index];
            NSString *imagelink=[dict valueForKey:@"link"];
            NSLog(@"imageLink %@",imagelink);
           [self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:        [UIImage imageNamed:@"1.png"]];
    
    
        }
     }