Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Austin Wyatt
3,146 PointsTouch anywhere on the screen to make an action happen
I am making an app for the blind and I need the user the be aloud to touch anywhere on the screen so that the appropriate action can happen.
1 Answer
Hayden Pennington
6,154 PointsSo, this is actually really easy. You use the UITapGestureRecognizer class. It can be accomplished in Interface Builder, but I prefer to do it in code, plus it will demonstrate how simple it can be done.
I would also like to note that the process is almost identical for other gestures. Like UIPinchGestureRecognizer or UISwipeGestureRecognizer.
I Highly recommend that you read the documentation for UITapGestureRecognizer, to see what all it has to offer.
Anyway, to the code. I usually set up the UITapGestureRecognizer in viewDidLoad, so that is what I will use in my example.
override func viewDidLoad() {
super.viewDidLoad()
// First create an instance of UITapGestureRecognizer
// Specify 'self' for the target, so it will send the instance of UITapGestureRecognizer
// And specify the method you want to execute for each tap. Note the colon ':' after the method.
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
// You can set the number of required taps. By default this is 1
tapGesture.numberOfTapsRequired = 1
// There are other properties, that you can set too. Like how many fingers have to be touching.
// Now add the gesture recognizer to your view
view.addGestureRecognizer(tapGesture)
}
// Each tap will call this method
func handleTapGesture(sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Ended {
// Do something...
}
}
Austin Wyatt
3,146 PointsAustin Wyatt
3,146 PointsHow do you connect that with the main storyboard though? How would I connect the code so that when you touch anywhere on the screen a certain action would happen that the user would be able to see?
Austin Wyatt
3,146 PointsAustin Wyatt
3,146 PointsHow do you connect that with the main storyboard though? How would I connect the code so that when you touch anywhere on the screen a certain action would happen that the user would be able to see?
Austin Wyatt
3,146 PointsAustin Wyatt
3,146 PointsHow do you connect that with the main storyboard though? How would I connect the code so that when you touch anywhere on the screen a certain action would happen that the user would be able to see?
Austin Wyatt
3,146 PointsAustin Wyatt
3,146 PointsHow do you connect that with the main storyboard though? How would I connect the code so that when you touch anywhere on the screen a certain action would happen that the user would be able to see?
Hayden Pennington
6,154 PointsHayden Pennington
6,154 PointsA UIViewController in your storyboard has the property 'view'. So, in the line view.addGestureRecognizer(tapGesture), 'view' is referring to the view in your storyboard.
Put a println ( i.e. println("screen touched") ) in 'handleTapGesture', to see if it works.
If your view hierarchy is more complex, you can create an IBOutlet from whichever view, in your storyboard, to your code. And then replace 'view' in view.addGestureRecognizer(tapGesture) with your your the name of your view.