Skip to content

Instantly share code, notes, and snippets.

@claymcleod
Last active August 29, 2015 14:15
Show Gist options
  • Save claymcleod/b46a1c84e39cddc868e7 to your computer and use it in GitHub Desktop.
Save claymcleod/b46a1c84e39cddc868e7 to your computer and use it in GitHub Desktop.
// Title: Listening for Tap Gestures
// Authors: Clay McLeod
// Description: How to handle tap gestures on a view controller.
// Section: Swift
// Subsection: UI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Create TapGestureRecognizer to listen for taps
// target: Which class contains your callback? (self means to the same class)
// action: Name of the callback method
var tgr : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchDidOccur:")
// Register our TapGestureRecognizer in our parent view
self.view.addGestureRecognizer(tgr)
...
}
...
func touchDidOccur(recognizer: UITapGestureRecognizer){
// handle touch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment