Swipe gesture between view controllers

Asim Zaidi picture Asim Zaidi · Oct 21, 2014 · Viewed 14.3k times · Source

I am trying to create a swipe on background. I have three different color of backgrounds. I have made a button and when I click my button my background changes to next scene. I want to change this and I want to swipe my thumb left or right to change the background and not to have to click on the button. How would I achieve that.

here are some more details

my view controller is

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

enter image description here

Answer

scottphc picture scottphc · Oct 21, 2014

Swift version:

Add gesture recognizer:

override func viewDidLoad() {
    super.viewDidLoad()
    let recognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeLeft:")
    recognizer.direction = .Left
    self.view .addGestureRecognizer(recognizer)
}

action function:

func swipeLeft(recognizer : UISwipeGestureRecognizer) {
    self.performSegueWithIdentifier("MySegue", sender: self)
}