IPhone: setting the required swipe length

I was wondering if there is an easy way to set the minimum length of the swipe, i.e. the length in pixel that the user must scroll so that this gesture is recognized as a swipe.

I noticed that a regular napkin can be completely immune (compared to scrolling through photos in your photo library, for example).

This is the usual way, but I would like to reduce the required scroll length:

- (void)viewDidLoad { // SWIPING GESTURES: UISwipeGestureRecognizer *swipeLeftRecognizer; swipeLeftRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundLeftSwipe:)]; swipeLeftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft; //swipeRecognizer.numberOfTouchesRequired=1; [self.view addGestureRecognizer:swipeLeftRecognizer]; [swipeLeftRecognizer release]; UISwipeGestureRecognizer *swipeRightRecognizer; swipeRightRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundRightSwipe:)]; swipeRightRecognizer.direction=UISwipeGestureRecognizerDirectionRight; //swipeRecognizer.numberOfTouchesRequired=1; [self.view addGestureRecognizer:swipeRightRecognizer]; [swipeRightRecognizer release]; [super viewDidLoad]; } #pragma mark - #pragma mark Swipes - (void)foundLeftSwipe:(UISwipeGestureRecognizer *)recognizer { // do something } - (void)foundRightSwipe:(UISwipeGestureRecognizer *)recognizer { // do something } 

I remember that there is a way to get the starting position of the pixel and the ending position, and then compare them, but it was just interesting if there was a simpler method, i.e. just by defining the value of the minimum swipe length in the code that I have.


EDIT:

Here's how I transcoded it all:

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { if (deltaXX > 0) { label.text = @"Horizontal Left swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } else { label.text = @"Horizontal Right swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } } if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { if (deltaYY > 0) { label.text = @"Vertical up swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } else { label.text = @"Vertical down swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } } } 
+4
source share
4 answers

Please check out this documentation http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

You can use touchhesBegan, touchsMoved and touchesEnded to find the start and end positions of the swipe. Having found the delta between the beginning and the end, you can get the length of the napkin.

Maybe something like this.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint gestureEndPoint = [touch locationInView:self.view]; // compare gestureStartPoint and gestureEndPoint to determine swipe length } 
+5
source

I came across the same and I was looking for a solution. UISwipeGestureRecognizer does not have such a thing as the required distance , but this is not all that you can use. I ended up using UIPanGestureRecognizer ! It looks like a super-class gesture recognizer, and it certainly provides more useful data.

There is a method available in this class called translationInView:(UIView *)aView that returns CGPoint , where x and y are the total distance over the entire gesture! Something surprisingly useful for our request! And he respects the direction in positive and negative values, which means: y = + 100 → swipe up to 100 points , x = + 100 swipe 100 points on the screen from left to right , etc.

What I do is check if the user was drawn for a certain amount of points (I was looking for something like scrolling down, so for me it was y> 150 ) and then do my thing ...

Here are some code excerpts:

  -(void)viewDidLoad { UIPanGestureRecoginzer *panDown=[[UIPanGestureRecoginzer alloc] initWithTarget:self action:@selector(swipedDown:)]; [self.view addGestureRecognizer:panDown]; } . . . -(void)swipedDown:(UIPanGestureRecoginzer *)recognizer { CGPoint panned=[recognizer translationInView:self.view]; if(panned.y>150){ //swiped down for 150 points doSomething(); } } 

It also gives you pan speed: velocityInView:

Note. If you want to have a general panning effect, you should use your view (self.view in most cases).

Hope this helps: UIPanGestureRecognize Class Reference

+5
source

Take a look at this question: UISwipeGestureRecognizer Mileage Length

+1
source

Swift 3:

 var gestureStartPoint: CGPoint = .zero override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first gestureStartPoint = touch!.location(in: self.view) } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first let gestureEndPoint = touch!.location(in: self.view) let dist = distance(gestureStartPoint, gestureEndPoint) print(dist) } func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat { let xDist = ax - bx let yDist = ay - by return CGFloat(sqrt((xDist * xDist) + (yDist * yDist))) } 
0
source

Source: https://habr.com/ru/post/1348158/


All Articles