What is the difference between Pan and Swipe in iOS?

后端 未结 4 781
半阙折子戏
半阙折子戏 2020-11-28 02:41

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

UISwipeG         


        
相关标签:
4条回答
  • 2020-11-28 03:09

    As per the apple document. Apple UIPanGestureRecognizer The difference between a pan and a swipe as below:-

    UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures. The user must be pressing one or more fingers on a view while they pan it. Clients implementing the action method for this gesture recognizer can ask it for the current translation and velocity of the gesture.

    A panning gesture is continuous. It begins (began) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (changed) when a finger moves while at least the minimum number of fingers are pressed down. It ends (ended) when all fingers are lifted.

    Clients of this class can, in their action methods, query the UIPanGestureRecognizer object for the current translation of the gesture (translation(in:)) and the velocity of the translation (velocity(in:)). They can specify the view whose coordinate system should be used for the translation and velocity values. Clients may also reset the translation to a desired value.

    Swift 3 UIPanGestureRecognizer Demo Example:- Resource Link

    import UIKit
    
    class ViewController: UIViewController {
    // this records our circle's center for use as an offset while dragging
    var circleCenter: CGPoint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Add a draggable view
        let circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
        circle.center = self.view.center
        circle.layer.cornerRadius = 50.0
        circle.backgroundColor = UIColor.green()
    
        // add pan gesture recognizer to
        circle.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragCircle)))
    
        self.view.addSubview(circle)
    }
    
    func dragCircle(gesture: UIPanGestureRecognizer) {
        let target = gesture.view!
    
        switch gesture.state {
        case .began, .ended:
            circleCenter = target.center
        case .changed:
            let translation = gesture.translation(in: self.view)
            target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)
        default: break
        }
      }
    }
    

    As per the apple document. Apple UITapGestureRecognizer

    UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

    Although taps are discrete gestures, they are discrete for each state of the gesture recognizer; thus the associated action message is sent when the gesture begins and is sent for each intermediate state until (and including) the ending state of the gesture. Code that handles tap gestures should therefore test for the state of the gesture.

    Swift 3 UITapGestureRecognizer Demo Example Resource Link

    override func viewDidLoad() {
       super.viewDidLoad()
    
       let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
       tap.numberOfTapsRequired = 2
       view.addGestureRecognizer(tap)
    }
    func doubleTapped() {
         // do something cool here
         print("Test TapGesture")
    }
    

    Example Image for Recognizer

    0 讨论(0)
  • 2020-11-28 03:15

    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.

    0 讨论(0)
  • 2020-11-28 03:28

    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.

    0 讨论(0)
  • 2020-11-28 03:30

    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..

    0 讨论(0)
提交回复
热议问题