Swift: browsing iOS using gestures

I am new to iOS development.

How can I implement swipe gestures to change the view back and forth? The best example I've seen so far is the Soundcloud app, but I couldn't figure out how to get it to work.

+3


source to share


3 answers


Use this code ...



override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)


}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right:

            println("Swiped right")

//change view controllers

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("StoryboardID") as ViewControllerName

        self.presentViewController(resultViewController, animated:true, completion:nil)    



        default:
            break
        }
    }
}

      

+6


source


You can use a UISwipeGestureRecognizer for your UIView and add a target and action to that gesture to be performed when the gesture occurs.



 var swipeGesture = UISwipeGestureRecognizer(target: self, action: "doSomething")
 myView.addGestureRecognizer(swipeGesture)

 func doSomething() {

    // change your view frame here if you want        
 }

      

0


source


This tutorial might be helpful to you: http://www.avocarrot.com/blog/implement-gesture-recognizers-swift/

Basically, you will need to add a gesture recognizer to your view that will listen for swipe gestures. Then, when he discovers the swipe, click on the next view.

-1


source







All Articles