First, create two methods, -nextImage and -prevImage and put in the code for displaying the next and previous images respectively.
Then add swipe gesture recognizers using the following code in -viewDidLoad:
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeImage:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[_imageParse addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeImage:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[_imageParse addGestureRecognizer:swipeRight];
To be able to show the images, your need to first send the whole array of data instead of the dictionary with the current item. For this, convert the _newsData property to an NSArray and where you set the current item's dictionary in the new viewController, set the whole data instead. Also, introduce a new variable. Like so:
@property (strong, nonatomic) NSArray *newsData;
@property NSInteger currentIndex;
Introduce a method for showing the image based on index
-(void)showImageAtIndex:(NSInteger)index
{
NSString *path=[[_newsData objectAtIndex:index] objectForKey:@"bigimg"];
NSData* imageData = [NSData dataWithContentsOfURL: [NSURL URLWithString: path]];
UIImage* image = [UIImage imageWithData: imageData];
NSLog(@"%@", image);
_imageParse.image = image;
}
Now, in your viewDidLoad method, set the current image as:
[self showImageAtIndex:_currentIndex];
The -swipeImage method should look something like this:
-(void)swipeImage:(UISwipeGestureRecognizer*)recognizer
{
NSInteger index = _currentIndex;
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
index++;
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
index--;
}
if (index > 0 || index < ([_newsData count] - 1))
{
_currentIndex = index;
[self showImageAtIndex:_currentIndex];
}
else
{
NSLog(@"Reached the end, swipe in opposite direction");
}
}
Here's what your -prepareForSegue method of the GalleryControllerCollection class should look like:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PatternView *cell = (PatternView *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
GalleryControllerFullmage *vc = (GalleryControllerFullmage*)segue.destinationViewController;
vc.newsData = [_patternImagesArray copy];
vc.currentIndex = indexPath.row;
}
Name the segue which transitions from the collection to the image view as "goToFullImage"
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"goToFullImage" sender:[collectionView cellForItemAtIndexPath:indexPath]];
}