Reputation: 13
I have a view that has a swipe recognizer that sends it to the moveToNextItem function:
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem))
swipe.direction = [.left, .right]
playCardView.addGestureRecognizer(swipe)
Currently the moveToNextItem function just handles both left and right swipes the same:
@objc func moveToNextItem() {
//Moves to next item
}
What is the right way to pass the direction that the user swiped to that function so it can move left or right. I could create 2 functions (one for left and one for right) but that seems like a bad way to do it. I'd like to just pass the direction to the function and let the function what should happen. Maybe in a way so the function could switch on the directions? Something like this:
@objc func moveToNextItem() {
switch direction{
case .left:
//left swipe action
case .right:
//right swipe action
default: //default
}
}
How would I go about doing this?
Upvotes: 1
Views: 6186
Reputation: 4855
You can make use of to methods
First Method
/// My Gesture For Multiple DIrection
private var mySwipeGesturec: UISwipeGestureRecognizer?
/// Directions Array
let directionsArray: [UISwipeGestureRecognizerDirection] = [.right, .left]
/// Create For Loop
for currentDirection in directionsArray {
mySwipeGesturec = UISwipeGestureRecognizer()
mySwipeGesturec = UISwipeGestureRecognizer(target: self, action: #selector(onScreenCodes.swipeHandler(_:)))
mySwipeGesturec?.direction = currentDirection
self.view.addGestureRecognizer(mySwipeGesturec!)
}
@objc func swipeHandler(_ sender: UISwipeGestureRecognizer){
switch sender.direction {
case .left:
print("Left Swipe")
case .right:
print("Right Swipe")
default:
break
}
}
Second Method
private var leftSwipeGesture : UISwipeGestureRecognizer?
private var rightSwipeGesture : UISwipeGestureRecognizer?
leftSwipeGesture = UISwipeGestureRecognizer()
leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(onScreenCodes.swipeHandler(_:)))
leftSwipeGesture?.direction = .left
self.view.addGestureRecognizer(leftSwipeGesture!)
rightSwipeGesture = UISwipeGestureRecognizer()
rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(onScreenCodes.swipeHandler(_:)))
rightSwipeGesture?.direction = .right
self.view.addGestureRecognizer(rightSwipeGesture!)
@objc func swipeHandler(_ sender: UISwipeGestureRecognizer){
switch sender.direction {
case .left:
print("Left Swipe")
case .right:
print("Right Swipe")
default:
break
}
}
Note Both are same its just Depends on Your requirement
Upvotes: -1
Reputation: 82759
you need to create the two separate gesture for each swipe event,reason is actually works in single event, for e.g
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
playCardView.addGestureRecognizer(leftSwipe)
playCardView.addGestureRecognizer(rightSwipe)
}
When a gesture is detected the moveToNextItem method is called, implement this method.
@objc func moveToNextItem(_ sender:UISwipeGestureRecognizer) {
switch sender.direction{
case .left:
//left swipe action
case .right:
//right swipe action
default: //default
}
}
Upvotes: 7