programmatically model swipe gesture in Swift

I implement swipe gesture recognizer in Swift. I want to be able to simulate a card toss (swipe your finger programmatically).

I assumed there would be a built-in function for this, but all I found is one for the touch gesture, not the swipe gesture.

This is how I implement the swipe gesture:

  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)
+4
source share
4 answers

You can create a UIPanGestureRecognizer yourself and pass it to a method wasDragged. You should check different translation values:

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

Although I suppose you need something else. Why would you simulate this gesture first?

+1

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
}
0

( , iOS ).

, , , , . :

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

:

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

activatedGestureRecognizer(gesture:) :

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

, . , , , .

, , , ?

0
source

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
    }
 }
}
-1
source

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


All Articles