What is the difference between Pan and Swipe in iOS?
Asked Answered
O

3

139

Sounds simple .. Hold the Trackpad, move the finger, release .. But somehow swipe is not being triggered (pan is triggered instead)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] 
      initWithTarget:v action:@selector(handleSwipe:)];
swipeGesture.direction= UISwipeGestureRecognizerDirectionUp;
[v addGestureRecognizer:swipeGesture];

Pan is recognized by the above sequence instead.

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] 
      initWithTarget:v action:@selector(handlePan:)];
[v addGestureRecognizer: panGesture];

If pan is commented, swipe is recognized by the same gesture .. With this, 2 questions:

  • What is the difference then between a pan and a swipe?
  • How can one simulate a swipe on iPhone simulator?
Orissa answered 27/3, 2012 at 22:27 Comment(1)
@rickster, correct me if I'm wrong, but swipe uses four directions, up, down, left, right, and pan can be any direction. CorrectIslas
B
248

By definition, a swipe gesture is necessarily also a pan gesture -- both involve translational movement of touch points. The difference is in the recognizer semantics: a pan recognizer looks for the beginning of translational movement and continues to report movement in any direction over time, while a swipe recognizer makes an instantaneous decision as to whether the user's touches moved linearly in the required direction.

By default, no two recognizers will recognize the same gesture, so there's a conflict between pan and swipe. Most likely, your pan recognizer "wins" the conflict because its gesture is simpler / more general: A swipe is a pan but a pan may not be a swipe, so the pan recognizes first and excludes other recognizers.

You should be able to resolve this conflict using the delegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:, or perhaps without delegation by making the pan recognizer depend on the swipe recognizer with requireGestureRecognizerToFail:.

With the conflict resolved, you should be able to simulate a one-finger swipe by quickly dragging the mouse. (Though as the mouse is more precise than your finger, it's a bit more finicky than doing the real thing on a device.) Two-finger pan/swipe can be done by holding the Option & Shift keys.

Balmuth answered 27/3, 2012 at 23:45 Comment(1)
Nice answer. But I don't think the pan gesture wins because it's more general, but rather because it's a continuous gesture (where a swipe is a discrete gesture) so a pan is recognised before a swipe. The swipe is only recognised on the finger being raised, the pan is recognised almost immediately after the finger starts moving. Swipe vs. Pan is covered well in the Event PG under "Declaring a specific order for two gesture recognizers".Tibbetts
S
29

Swipe Gesture will work when you drag your finger only in certain directions (swipe up,swipe down,swipe left, swipe right). For example swipeable cells in table view controller.

Pan Gesture will work when you drag your finger in any directions. You can give acceleration or deceleration to it. FOr example, moving a object from one place to another place or spinning a spinner..

Synthesize answered 13/1, 2015 at 4:35 Comment(0)
N
0

According to http://hammerjs.github.io/, I think the difference is:

  • pan: move directions in the same big view
  • swipe: switch between several views

The gesture is the same, both use one finger and move.

Nerti answered 22/6, 2016 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.