iamruskie
iamruskie

Reputation: 766

Proper Swipe gesture recognizer iOS

I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS. I need to detect swipe up & down, and the callbacks for them.

Any help, appreciated. Thanks.

Upvotes: 18

Views: 20545

Answers (2)

MB_iOSDeveloper
MB_iOSDeveloper

Reputation: 4198

This worked for me in Xcode 7.3. , Swift 2.2.

import UIKit

class Viewcontroller: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        createAndAddSwipeGesture()
    }

    private func createAndAddSwipeGesture()
    {
        let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(Viewcontroller.handleSwipeLeft(_:)))
        swipeGesture.direction = UISwipeGestureRecognizerDirection.Left
        view.addGestureRecognizer(swipeGesture)
    }

    @IBAction func handleSwipeLeft(recognizer:UIGestureRecognizer)
    {
        print(" Handle swipe left...")

    }
}

Upvotes: 0

sergio
sergio

Reputation: 69027

You need two recognizers, one for swiping up, and the other for swiping down:

UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;

and for the handler:

- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {

}

Finally, you add it to your view:

[view addGestureRecognizer:swipeUpGestureRecognizer];

The same for the other direction (just change all the Ups to Downs).

Upvotes: 51

Related Questions