some_id
some_id

Reputation: 29896

Setting a UIImageView with swipes

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

Upvotes: 0

Views: 3582

Answers (2)

christo16
christo16

Reputation: 4843

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.

Upvotes: 4

Philipp Schlösser
Philipp Schlösser

Reputation: 5219

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.

Upvotes: 1

Related Questions