Jitendra Jitendra - 4 months ago 5
iOS Question

Swipegesture Recognize in IOS

I am new to iOS.

I am developing an app where I have 2

. I want to load the array images into

I want to take the first image and add it to an
. When the use swipes to the left, I then want to add the next image from the array in to a new
and show that. If they swipe back, load the previous e.t.c

I also would like to load an image from the second array in to an
when a user double-taps an image.

i tried this one

AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

NSLog(@"%lu",(unsigned long)[delegate.FrontsCards count]);
ImgView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

//ImgView.image=[UIImage imageNamed:@"cloub1.png"];

int random=arc4random()%[delegate.FrontsCards count];

ImgView.image=[delegate.FrontsCards objectAtIndex:random];
[self.view addSubview:ImgView];
currentImageIndex = 0;
ImgView.userInteractionEnabled = YES;

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];

[super viewDidLoad];


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
NSLog(@"Swipe received.");

AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

for(int i=0;i<52;i++)
ImgView.image=[delegate.FrontsCards objectAtIndex:i];

ImgView.image=[UIImage imageNamed:@"cloub3.png"];

Could anyone help me do this? Thank you in advance.

Rob Rob

A couple of approaches:

  1. Swipe gesture: If you want to write your own swipe gestures, you might do something like this:

    • Define properties not only the array of image names, but also an index that shows you which card you're looking at:

      @property (nonatomic, strong) NSMutableArray *imageNames;
      @property (nonatomic) NSInteger imageIndex;
    • Add the gestures to your image view:

      UISwipeGestureRecognizer *swipe;
      // by the way, if not using ARC, make sure to add `autorelease` to
      // the following alloc/init statements
      swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
      swipe.direction = UISwipeGestureRecognizerDirectionLeft;
      [self.imageView addGestureRecognizer:swipe];
      swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
      swipe.direction = UISwipeGestureRecognizerDirectionRight;
      [self.imageView addGestureRecognizer:swipe];
    • Write the gesture recognizer handler:

      - (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
          UIViewAnimationOptions option = kNilOptions;
          if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
              // if we're at the first card, don't do anything
              if (self.imageIndex == 0)
              // if swiping to the right, maybe it's like putting a card
              // back on the top of the deck of cards
              option = UIViewAnimationOptionTransitionCurlDown;
              // adjust the index of the next card to be shown
          else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
              // if we're at the last card, don't do anything
              if (self.imageIndex == ([self.imageNames count] - 1))
              // if swiping to the left, it's like pulling a card off the 
              // top of the deck of cards
              option = UIViewAnimationOptionTransitionCurlUp;
              // adjust the index of the next card to be shown
          // now animate going to the next card; the view you apply the 
          // animation to is the container view that is holding the image
          // view. In this example, I have the image view on the main view,
          // but if you want to constrain the animation to only a portion of
          // the screen, you'd define a simple `UIView` that is the dimensions
          // that you want to animate and then put the image view inside
          // that view, and replace the `self.view` reference below with the
          // view that contains the image view.
          [UIView transitionWithView:self.view
                              self.imageView.image = [UIImage imageWithContentsOfFile:self.imageNames[self.imageIndex]];
  2. Obviously, if you want to write your own double tap gesture, go ahead and create a gesture for that, as well as a handler for that gesture, too, e.g.:

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 2;
    [self.imageView addGestureRecognizer:tap];
  3. As others have suggested, if you don't want to write your own image handling code like above, you could use a variety of other controls, e.g. a UIPageViewController, a UICollectionView, or any of a dozen other classes that facilitate navigation between a group of images.