I have added swipe action to my application. I have 5 buttons which represent days of the week. When I click on one button it changes the array of my table view and some things. Swipe action does exactly the same and works great but on swipe sometimes it freezes and works again after some seconds. it's latency.
override func viewDidLoad() {
super.viewDidLoad()
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(actionSwipeRight))
rightSwipe.direction = .right
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(actionSwipeLeft))
leftSwipe.direction = .left
view.addGestureRecognizer(rightSwipe)
view.addGestureRecognizer(leftSwipe)
}
And the swipe code
// s'éxécute lorsque qu'un swipe vers la droite est effectué
@objc private func actionSwipeRight() {
print("swipe right")
switch self.selectedDayinHoraire {
case "LUNDI" :
return
case "MARDI" :
changeDay(for: "LUNDI")
case "MERCREDI" :
changeDay(for: "MARDI")
case "JEUDI" :
changeDay(for: "MERCREDI")
case "VENDREDI" :
changeDay(for: "JEUDI")
default: break
}
}
// s'éxécute lorsque qu'un swipe vers la gauche est effectué
@objc private func actionSwipeLeft() {
print("swipe left")
switch self.selectedDayinHoraire {
case "LUNDI" :
changeDay(for: "MARDI")
case "MARDI" :
changeDay(for: "MERCREDI")
case "MERCREDI" :
changeDay(for: "JEUDI")
case "JEUDI" :
changeDay(for: "VENDREDI")
case "VENDREDI" :
return
default: break
}
}
func changeDay(for day : String) {
switch day {
case "LUNDI" :
self.menuSelectionné(btn: self.btnLundi)
self.LabelDynamiqueJour.text = " horaire du lundi"
self.selectedDayinHoraire = "LUNDI"
case "MARDI" :
self.menuSelectionné(btn: self.btnMardi)
self.LabelDynamiqueJour.text = " horaire du Mardi"
self.selectedDayinHoraire = "MARDI"
case "MERCREDI" :
self.menuSelectionné(btn: self.btnMercredi)
self.LabelDynamiqueJour.text = " horaire du Mercredi"
self.selectedDayinHoraire = "MERCREDI"
case "JEUDI" :
self.menuSelectionné(btn: self.btnJeudi)
self.LabelDynamiqueJour.text = " horaire du Jeudi"
self.selectedDayinHoraire = "JEUDI"
case "VENDREDI" :
self.menuSelectionné(btn: self.btnVendredi)
self.LabelDynamiqueJour.text = " horaire du vendredi"
self.selectedDayinHoraire = "VENDREDI"
default: break
}
tableviewHoraire.reloadData()
}
The buttons actions :
@IBAction func btnLun(_ sender: UIButton) {
changeDay(for: "LUNDI")
}
@IBAction func btnMar(_ sender: UIButton) {
changeDay(for: "MARDI")
}
@IBAction func btnMer(_ sender: UIButton) {
changeDay(for: "MERCREDI")
}
@IBAction func btnJeu(_ sender: UIButton) {
changeDay(for: "JEUDI")
}
@IBAction func btnVen(_ sender: UIButton) {
changeDay(for: "VENDREDI")
}
You can see swipe action do the same but sometimes it freezes, test it yourself if you want, the problem comes from Horaire view. https://github.com/CedricLnx/School-Companion
Set your Horaire class as subclass of UIGestureRecognizerDelegate
.
class Horaire: ViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate {
Set each Swipe's delegate to self.
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(actionSwipeRight))
rightSwipe.direction = .right
rightSwipe.delegate = self
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(actionSwipeLeft))
leftSwipe.direction = .left
leftSwipe.delegate = self
Add UIGestureRecognizerDelegate Function.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
As explained in Apple Docs shouldRecognizeSimultaneouslyWith allows two or more gestures to work at the sametime. In this case it should allow a single touch to be registered as a swipe on self.view as well as on tableView.