A couple of approaches:
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) return; // 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 self.imageIndex--; } else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) { // if we're at the last card, don't do anything if (self.imageIndex == ([self.imageNames count] - 1)) return; // 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 self.imageIndex++; } // 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 duration:0.5 options:option animations:^{ self.imageView.image = [UIImage imageWithContentsOfFile:self.imageNames[self.imageIndex]]; } completion:nil]; }
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];
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
, aUICollectionView
, or any of a dozen other classes that facilitate navigation between a group of images.