UISwipeGestureRecognizer does not work with swipeGesture.direction = .right

For the UISwipeGestureRecognizer, when I use direction = .left, it works.

However, when I use direction = .right, it does not work

Code Block swift
// swipeGesture is the UISwipeGestureRecognizer
if (swipeGesture.direction = .right) {
// Code does not run
} else if (swipeGesture.direction = .left) {
// Code runs
}

Does anyone have any idea why this happens?

How do you test ? = and not == ???

Should write:

Code Block
if swipeGesture.direction == .right {
// Code should run
} else if swipeGesture.direction == .left {
// Code runs
}


A switch case could be more appropriate here

Code Block
switch swipeGesture.direction {
case .right : print("Right")
case .left : print("Left")
case .up : print("Up")
case .down : print("Down")
}

Right sorry, there was a mistake in the code I wrote in my original post. It was '==' instead of '=' in my own code. Thanks for pointing that out.
However, a switch case still did not solve the issue
You need to add a UISwipeGestureRecognizer for each direction:
Code Block
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let rightSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
rightSwipeRecognizer.direction = .right
mySwipableView.addGestureRecognizer(rightSwipeRecognizer)
let leftSwipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
leftSwipeRecognizer.direction = .left
mySwipableView.addGestureRecognizer(leftSwipeRecognizer)
}
@objc func swipeRight(_ swipeGesture: UISwipeGestureRecognizer) {
print("right")
}
@objc func swipeLeft(_ swipeGesture: UISwipeGestureRecognizer) {
print("left")
}


I do not know how you have set direction of the UISwipeGestureRecognizer, but as far as I tested, you cannot detect swipe gestures for multiple directions with one UISwipeGestureRecognizer.
The following switch code
Code Block
switch swipeGesture.direction { }


should be for each action of the gesture you define.
UISwipeGestureRecognizer does not work with swipeGesture.direction = .right
 
 
Q