8

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
  • Does the view move at all with the code you currently ahve? – NSGangster Aug 15 '16 at 11:00
  • 1
    So you want do what the swipe gesture does programmatically? Why don't you just call the same function in both swipe, and manual? The offsets and positions would have to be set manually too of course. – Imbue Aug 15 '16 at 11:01
  • @NSGangster Yes it moves and swipes fine. – Nicholas Muir Aug 15 '16 at 13:19
  • @Imbue I am not sure what you mean by call it manually? – Nicholas Muir Aug 15 '16 at 13:20
  • I mean. If you want to "simulate" the flinging of the card, you can pass some random values to the function actually flinging the card, to make the simulation. And then just call it. Or have I misunderstood? – Imbue Aug 15 '16 at 13:23
  • 1
    @Imbue the function gets the offset from center by the swipe gesture (not the other way round). I was hoping there was an inbuilt function in gesture recognizer that I did not know about. Thanks anyway. – Nicholas Muir Aug 15 '16 at 13:30

5 Answers5

1

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?

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • If you want to reveal the swipe left/right action buttons, simulating the gesture would be a simple approach (though I am not sure we could replace the one created by apple) – maninvan Mar 17 '22 at 01:49
1

Here is code you can use to programatically swipe the cell. When performed, the cell will animate to the swiped position.

@IBAction
func swipeFirstCell() {
    if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
        tableView.perform(Selector("_endSwipeToDeleteRowDidDelete:"), with: nil) // cancel any existing swipes
        tableView.perform(Selector("_swipeToDeleteCell:"), with: cell)
    }
}

Note you need to also return true for the cell from canEditRowAtIndexPath.

This might get flagged by Apple's private API detection but you can at least use it for development and perhaps obfuscate the string if you really to.

malhal
  • 26,330
  • 7
  • 115
  • 133
0

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?

mohsen
  • 4,698
  • 1
  • 33
  • 54
hris.to
  • 6,235
  • 3
  • 46
  • 55
  • 1
    So when they hit the button in flings the card of the deck. Same thing as tinder does when you press the buttons like or dislike down the bottom instead of swiping. – Nicholas Muir Aug 15 '16 at 14:31
  • 1
    So you know exactly by how much you wan't to flick your card. Why not applying this directly to the cards instead of creating gesture that simulates this exact level of swiping? – hris.to Aug 15 '16 at 14:33
  • I want to be able to do both press button or flick. But for the button, the thing with a flick it follows a weird curve not sure how to do that and thought there might be an inbuilt method of some kind. – Nicholas Muir Aug 15 '16 at 14:35
0

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
}
Single Entity
  • 2,925
  • 3
  • 37
  • 66
Berlin
  • 2,115
  • 2
  • 16
  • 28
-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
    }
 }
}
iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • 1
    I already have the swipe working. I couldn't see anywhere in what you posted where it simulated the user swiping? Did I miss something. – Nicholas Muir Aug 15 '16 at 10:52