How to Detect Swipe Gesture in iOS

how detect swipe gesture direction?

The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.

The easiest would be to use two separate gesture recognizers instead. You could also inspect the location of the touch when the gesture starts and when it ends with the locationInView: method.

How to detect Swiping UP, DOWN, LEFT and RIGHT with SwiftUI on a View

Based on Benjamin's answer this is a swiftier way to handle the cases

.gesture(DragGesture(minimumDistance: 3.0, coordinateSpace: .local)
.onEnded { value in
print(value.translation)
switch(value.translation.width, value.translation.height) {
case (...0, -30...30): print("left swipe")
case (0..., -30...30): print("right swipe")
case (-100...100, ...0): print("up swipe")
case (-100...100, 0...): print("down swipe")
default: print("no clue")
}
}
)

How to detect Swipe Gesture in iOS?

Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videos on the subject even. Sessions 120 and 121. :)

Detect a finger swipe through JavaScript on the iPhone and Android

Simple vanilla JS code sample:

document.addEventListener('touchstart', handleTouchStart, false);        
document.addEventListener('touchmove', handleTouchMove, false);

var xDown = null;
var yDown = null;

function getTouches(evt) {
return evt.touches || // browser API
evt.originalEvent.touches; // jQuery
}

function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
};

function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}

var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;

var xDiff = xDown - xUp;
var yDiff = yDown - yUp;

if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* right swipe */
} else {
/* left swipe */
}
} else {
if ( yDiff > 0 ) {
/* down swipe */
} else {
/* up swipe */
}
}
/* reset values */
xDown = null;
yDown = null;
};

Tested in Android.

swipe gesture in touches methods in swift

Here's an example of how to detect a swipe gesture.

First, define instance variables to store the starting location and time

var start:(location:CGPoint, time:TimeInterval)?

and define parameters of the swipe gesture. Adjust these accordingly:

let minDistance:CGFloat = 25
let minSpeed:CGFloat = 1000
let maxSpeed:CGFloat = 6000

In touchesBegan, save the location/time of each new touch event

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
start = (touch.location(in:self), touch.timestamp)
}
}

In touchesEnded, determine if the user's gesture was a swipe by comparing the distance and speed of the gesture. The tests for diagonal swipes are optional.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
var swiped = false
if let touch = touches.first, let startTime = self.start?.time,
let startLocation = self.start?.location {
let location = touch.location(in:self)
let dx = location.x - startLocation.x
let dy = location.y - startLocation.y
let distance = sqrt(dx*dx+dy*dy)

// Check if the user's finger moved a minimum distance
if distance > minDistance {
let deltaTime = CGFloat(touch.timestamp - startTime)
let speed = distance / deltaTime

// Check if the speed was consistent with a swipe
if speed >= minSpeed && speed <= maxSpeed {

// Determine the direction of the swipe
let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0
let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0

swiped = true
switch (x,y) {
case (0,1):
print("swiped up")
case (0,-1):
print("swiped down")
case (-1,0):
print("swiped left")
case (1,0):
print("swiped right")
case (1,1):
print("swiped diag up-right")
case (-1,-1):
print("swiped diag down-left")
case (-1,1):
print("swiped diag up-left")
case (1,-1):
print("swiped diag down-right")
default:
swiped = false
break
}
}
}
}
start = nil
if !swiped {
// Process non-swipes (taps, etc.)
print("not a swipe")
}
}

How to detect swipe direction?

If you want to know the direction when the gesture is not finished you would probably need the UIPanGestureRecognizer and detect the direction using the velocityInView: method.

in the *.h file:

@interface ViewController : UIViewController

@property (nonatomic, strong) UIPanGestureRecognizer *recognizer;

@end

in the *.m file:

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

[self.view addGestureRecognizer:self.recognizer];
}

-(void)panGesture:(UIPanGestureRecognizer *)sender
{
NSLog(@"Velocity: %@", NSStringFromCGPoint([sender velocityInView:self.view]));

// Here You need to determine the direction
}
@end

IOS: Detect swipe gestures in a specific place on a UIView

I am not sure if this will help you or you tried something like this, but i want to share my idea.

You can try UISwipeGestureRecognizer in your ViewControl.m like below:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToDoMethod)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
[[self innerView] addGestureRecognizer: swipeGesture];

You can add an inner view into your main view and add this gesture to that view.

Hope this helps you, good luck! :)

How to recognize swipe in all 4 directions

You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:

override func viewDidLoad() {
super.viewDidLoad()

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

switch swipeGesture.direction {
case .right:
print("Swiped right")
case .down:
print("Swiped down")
case .left:
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}

Swift 3:

override func viewDidLoad() {
super.viewDidLoad()

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}

How to detect swipe direction, touch position, etc?

One possibility: Divide your interface into views (which could be invisible), one gesture recognizer per view. The gesture recognizer responds if the gesture is made in its view.

Another possibility: The whole interface is one big view with just one gesture recognizer, and you analyze the touches to figure out exactly where the gesture took place.



Related Topics



Leave a reply



Submit