programmatically simulate a swipe gesture in Swift

2019-06-21 18:19发布

问题:

I am implementing a gesture recognizer for swiping in Swift. I wan to be able to simulate the flinging of the card (programmatically swipe the view).

I assumed there would be a built in function for this but all I have found is one for tap gesture not swipe gesture.

This is how I am implementing the swipe gesturing:

  let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
    cardView.addGestureRecognizer(gesture)
    cardView.userInteractionEnabled = true
}

func wasDragged (gesture: UIPanGestureRecognizer) {        
    let translation = gesture.translationInView(self.view)
    let cardView = gesture.view!

    // Move the object depending on the drag position
    cardView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
                              y:  self.view.bounds.height / 2 + translation.y)

回答1:

You can create the UIPanGestureRecognizer by yourself and pass it to the wasDragged method. You should check with different values of the translation though:

let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: self.view)
wasDragged(gesture)

SWIFT 4.2

 let gesture = UIPanGestureRecognizer()
 gesture.setTranslation(CGPoint(x: 0, y: 100), in: self.view)
 wasDragged(gesture)

Although I asume you need something else. Why do you need to simulate this gesture in the first place?



回答2:

For SWIFT 3.0

let swipeRightOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = .Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = .Left;

@IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blue
}
@IBAction func slideToRightWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGray
}


回答3:

You can't simulate a gesture recognizer in its full implications (I mean, you can't actually make iOS think it's a real user action).

You can, however, fool your own code making it act as if it were a real swipe. For that, you need to create a gesture recognizer first:

var gestureRecognizerSwipeRight = UISwipeGestureRecognizer(target: self, action: "activatedGestureRecognizer:")
gestureRecognizerSwipeRight.direction = UISwipeGestureRecognizerDirection.Right
yourView.addGestureRecognizer(gestureRecognizerSwipeRight)

And then pass it directly to your action:

// Some other place in your code
self.activatedGestureRecognizer(gesture: gestureRecognizerSwipeRight)

Your activatedGestureRecognizer(gesture:) method should be something like:

func activatedGestureRecognizer(gesture: UIGestureRecognizer) {
    if let gestureRecognizer = gesture as? UIGestureRecognizer {

        // Here you can compare using if gestureRecognizer == gestureRecognizerSwipeRight
        // ...or you could compare the direction of the gesture recognizer.
        // It all depends on your implementation really.

        if gestureRecognizer == gestureRecognizerSwipeRight {
            // Swipe right detected
        }
    }
}

In fairness, I don't see any real gain in doing it this way. It should be a lot better to simply do the action associated with the swipe instead of actually simulating the gesture recognizer.

If you need, for instance, to animate your card while swipping, why don't you simply disable the user interaction on your card view and animate it programmatically?



回答4:

You need to implement UISwipeGestureRecognizer

override func viewDidLoad() {
super.viewDidLoad()

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)

var swipeDown = UISwipeGestureRecognizer(target: self, action: "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
    }
 }
}