views:

67

answers:

2

I currently use a left and right arrow to switch between images, but would like to add functionality so that the user can swipe in the direction to change the image.

How does the device detect a left swipe or right swipe and use that as an IBAction or similar to the button triggering execution?

Is there a build in method or does it need to be coded from scratch?

Thanks

+1  A: 

If your target is 3.2 or higher, you can use UISwipeGestureRecognizer. Put this in your UIImageView subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    [recognizer setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:recognizer];
    [recognizer release];
}

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
     //do something
}

If you're targeting 3.2 or lower, you'll need to make use of NSClassFromString & [[UIDevice currentDevice] systemVersion] to make sure the current device has the required class.

christo16
Thanks, how do I do it for the left side? Do I just duplicate the methods and change the name?
alJaree
Right, so create another UISwipeGestureRecognizer and set the direction to UISwipeGestureRecognizerDirectionLeft.
christo16
+1  A: 

In addition to what christo said, I would still recommend to do this the "before 3.2"-Way since you won't be able to support iPhones, which have not yet updated to 4.0, so consequently every 1. gen iPhone out there.

The old way is to overwrite touchesBegan/Moved/Ended in your UIImageView subclass. This post is about TableViews but the exact same code works for every subclass of UIView, so you can use it in your ImageView.

Phlibbo
thank you for the link.
alJaree