iOS Gestures in Swift – How to

iOS Gestures in Swift

iOS gestures can be useful in your app. And here is a short demonstration on how to implement iOS gestures in your app.

It seems like iOS gestures require one recogniser for each direction.


//Recogniser
let swipeRight = UISwipeGestureRecognizer(target: self, action: "respond:")
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)

respond is the name of the handler or function to run the code. So anything you want happening when your user is swiping goes into that function. Like this, just an example to change the text in a label.


// Respond function
func respond(gesture: UIGestureRecognizer) {
lblDirection.text = "Right"
}

If you add recognisers for all four direction, you can still point all recogniser to the same function. And use something like this to check for what direction the user swiped.


// Respond function with all four directions
func respond(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
lblDirection.text = "Right"
case UISwipeGestureRecognizerDirection.Left:
lblDirection.text = "Left"
case UISwipeGestureRecognizerDirection.Up:
lblDirection.text = "Up"
case UISwipeGestureRecognizerDirection.Down:
lblDirection.text = "Down"
default:
break
}
}
}

That is how you implant iOS gestures. You can also download a working app with this code from github using this link: https://github.com/solron/Swipe-Gesture

If you want to see in a video how to do this. You can check out my video here on youtube.https://www.youtube.com/watch?v=EzBnQg6ZKrs

Thats it really!

Happy coding!

Related Posts

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

csharp ping check

C# Ping Check the easy way

How can we check if a computer, server, or another device is online? We can use a C# Ping check. If you can check the device with…

C# return index of a object

C# return index of a object in a List

How to return the index of a list type that is an object in C#. Most examples only shows simple string types when they show how to…

c# 99 bottles of beer

C# 99 bottles of beer on the wall

C# 99 bottles of beer on the wall is an easy programming challenge. The challenge can be solved in at least dozens of ways. Here is one…

Leave a Reply