1

I am working on a food app in which each tableViewCell shows different recipes names. When a user taps on a recipe (in a cell), a detailed view will be opened.

I am now trying to implement a swipe gesture for UIImageView which shows different images on swipe. How do I implement that?

 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];


 // Setting the swipe direction.
 [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
 [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
 // Adding the swipe gesture on image view

 [Allimages addGestureRecognizer:swipeLeft];
 [Allimages addGestureRecognizer:swipeRight];



- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe{

 NSInteger indexPath;

 if (indexPath==0) {

 if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {


   arrayimage=[[NSMutableArray alloc]initWithObjects:@ "BBQ Chicken Pizza.jpg",@ "roastchickensteaks.jpg", nil];


   swipee =  (swipee > 0)?  ([arrayimage count]-1):
    swipee%[arrayimage count];


    Allimages.image=[UIImage imageNamed:  [arrayimageobjectAtIndex:indexPath]];
    }}

https://i.stack.imgur.com/apDEw.png

https://i.stack.imgur.com/EiURG.png

https://i.stack.imgur.com/1q799.png

UdayM
  • 1,725
  • 16
  • 34
Rana Raza
  • 41
  • 7

2 Answers2

1

Add this

 [imageview setUserInteractionEnabled=YES];

create object for swipegesture recognizer class

UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe)];

[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];

Add swipeGesture to imageView

[imageView addGestureRecognizer:leftSwipe];

-(void)onSwipe:(UISwipeGestureRecognizer)swipe{
 if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
  {
   NSLog(@"leftSwipe");
  }
}

if you want more information: just check Gesture recognizer (swipe) on UIImageView

Community
  • 1
  • 1
UdayM
  • 1,725
  • 16
  • 34
  • if you found useful accept this answer..so that it will be helpful for more users – UdayM Mar 04 '16 at 05:29
  • But when i implement this code every cell category get theses images.which is show on first. – Rana Raza Mar 04 '16 at 05:55
  • it means each cell showing same images. your line is not clear – UdayM Mar 04 '16 at 06:00
  • yes ur right . i want different cell have different images.Then 2nd cell show different images. Look at this link – Rana Raza Mar 04 '16 at 06:10
  • yes ur right . i want different cell have different images.Then 2nd cell show different images. Look at this link..see in Features. http://codecanyon.net/item/ios-recipe-app/10960556 Multiple images per recipe. – Rana Raza Mar 04 '16 at 06:32
  • add your code.. and simulator images to the question .Then it will be easy to answer – UdayM Mar 04 '16 at 06:40
0

In your detail View Make UserInteractionEnabledt to YES of your imageview.

 [imageView setUserInteractionEnabled:YES];

Adding a Swipe Gesture Events

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeLeft];
[imageView addGestureRecognizer:swipeRight];

And Handle Swipe Gesture Events using below code.

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");   
    } 

}

Or else You can use Third Party Tool

https://www.cocoacontrols.com/controls/pagedflowview

  • i want different cell have different images.Then 2nd cell show different images. Look at this link..see in Features. http://codecanyon.net/item/ios-recipe-app/10960556 Multiple images per recipe; – Rana Raza Mar 04 '16 at 06:36