Any idea if there is a way to get the length of a swipe gesture or the touches so that i can calculate the distance?
Asked
Active
Viewed 3.4k times
37
-
I think you can only get direction from UISwipeGestureRecognizer. Maybe you can get the position where the touch begin and where it ends and calculate the lengh from that. – picknick Jan 28 '11 at 14:17
6 Answers
58
It's impossible to get a distance from a swipe gesture, because the SwipeGesture triggers the method where you could access the location exactly one time, when the gesture has ended.
Maybe you want to use a UIPanGestureRecognizer.
If it possible for you to use pan gesture you would save the starting point of the pan, and if the pan has ended calculate the distance.
- (void)panGesture:(UIPanGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
startLocation = [sender locationInView:self.view];
}
else if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint stopLocation = [sender locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
CGFloat dy = stopLocation.y - startLocation.y;
CGFloat distance = sqrt(dx*dx + dy*dy );
NSLog(@"Distance: %f", distance);
}
}

Matthias Bauch
- 89,811
- 20
- 225
- 247
-
10If using a UIPanGestureRecognizer, the built in method - (CGPoint)translationInView:(UIView *)view works very well. – Jon Aug 06 '13 at 18:30
-
2You'd certainly use translationInView here. Perhaps MB could edit the answer one day, since, it's such a high-marker! Eg http://stackoverflow.com/questions/15888276/with-uipangesturerecognizer-is-there-a-way-to-only-act-so-often-like-after-x-m Rock on... – Fattie Feb 22 '14 at 10:15
-
16
In Swift
override func viewDidLoad() {
super.viewDidLoad()
// add your pan recognizer to your desired view
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panedView))
self.view.addGestureRecognizer(panRecognizer)
}
@objc func panedView(sender:UIPanGestureRecognizer){
var startLocation = CGPoint()
//UIGestureRecognizerState has been renamed to UIGestureRecognizer.State in Swift 4
if (sender.state == UIGestureRecognizer.State.began) {
startLocation = sender.location(in: self.view)
}
else if (sender.state == UIGestureRecognizer.State.ended) {
let stopLocation = sender.location(in: self.view)
let dx = stopLocation.x - startLocation.x;
let dy = stopLocation.y - startLocation.y;
let distance = sqrt(dx*dx + dy*dy );
NSLog("Distance: %f", distance);
if distance > 400 {
//do what you want to do
}
}
}
Hope that helps all you Swift pioneers

Developer6479
- 5
- 3

Chris Klingler
- 5,258
- 2
- 37
- 43
-
2for lazy people you should also declare `var startLocation = CGPoint()` in your class – Radu Ursache Apr 12 '18 at 12:06
-
agreed @RaduUrsache, there is no way to store the start location in this solution above. – Mark Perkins Jan 24 '20 at 20:19
4
func swipeAction(gesture: UIPanGestureRecognizer) {
let transition = sqrt(pow(gesture.translation(in: view).x, 2)
+ pow(gesture.translation(in: view).y, 2))
}

Massimiliano Kraus
- 3,638
- 5
- 27
- 47

Møhãməđ Đąŕwîčh
- 41
- 3
-
4Please note: code only answers are discouraged. Please add a bit of explanation. – GhostCat Aug 25 '17 at 11:31
-
Its the equation to calculate distance between two points. https://www.mathsisfun.com/algebra/distance-2-points.html – Hari Kunwar Dec 12 '18 at 21:04
3
For those of us using Xamarin:
void panGesture(UIPanGestureRecognizer gestureRecognizer) {
if (gestureRecognizer.State == UIGestureRecognizerState.Began) {
startLocation = gestureRecognizer.TranslationInView (view)
} else if (gestureRecognizer.State == UIGestureRecognizerState.Ended) {
PointF stopLocation = gestureRecognizer.TranslationInView (view);
float dX = stopLocation.X - startLocation.X;
float dY = stopLocation.Y - startLocation.Y;
float distance = Math.Sqrt(dX * dX + dY * dY);
System.Console.WriteLine("Distance: {0}", distance);
}
}

IceWarrior353
- 99
- 1
- 8
2
You can only do it a standard way: remember the touch point of touchBegin and compare the point from touchEnd.

Oleg Danu
- 4,149
- 4
- 29
- 47
0
I have an implementation similar to the answer in swift that discriminates between a drag and a swipe calculating the distance relative to the container and the speed of the swipe.
@objc private func handleSwipe(sender: UIPanGestureRecognizer) {
if (sender.state == .began) {
self.swipeStart.location = sender.location(in: self)
self.swipeStart.time = Date()
}
else if (sender.state == .ended) {
let swipeStopLocation : CGPoint = sender.location(in: self)
let dx : CGFloat = swipeStopLocation.x - swipeStart.location.x
let dy : CGFloat = swipeStopLocation.y - swipeStart.location.y
let distance : CGFloat = sqrt(dx*dx + dy*dy );
let speed : CGFloat = distance / CGFloat(Date().timeIntervalSince(self.swipeStart.time))
let portraitWidth = min(self.frame.size.width, self.frame.size.height)
print("Distance: \(distance), speed: \(speed), dy: \(dy), dx: \(dx), portraitWidth: \(portraitWidth), c1: \(distance > portraitWidth * 0.4), c2: \(abs(dy) < abs(dx) * 0.25), c3: \(speed > portraitWidth * 3.0) ")
if distance > portraitWidth * 0.4 && abs(dy) < abs(dx) * 0.25 && speed > portraitWidth * 3.0 {
if dx > 0 {
delegate?.previousAssetPressed(self)
}else{
delegate?.nextAssetPressed(self)
}
}
}
}

Carles Estevadeordal
- 1,137
- 9
- 17