滑动动作延迟

Swipe Action latency

我在我的应用程序中添加了滑动操作。我有 5 个按钮,代表一周中的几天。当我点击一个按钮时,它改变了我的 table 视图的数组和一些东西。 滑动操作完全相同并且效果很好但滑动有时会冻结并在几秒钟后再次工作。这是延迟。

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

以及扫码

// 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()
}

按钮操作:

@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")
}

可以看到swipe action也是一样的,但是有时候会卡顿,需要的话自己测试一下,问题出在Horaire view上。 https://github.com/CedricLnx/School-Companion

将你的 Horaire class 设置为 UIGestureRecognizerDelegate 的子class。

class Horaire: ViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate {

将每个 Swipe 的委托设置为自己。

    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

添加 UIGestureRecognizerDelegate 函数。

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Apple Docs 中所述,shouldRecognizeSimultaneouslyWith 允许同时使用两个或多个手势。在这种情况下,它应该允许在 self.view 和 tableView 上将单次触摸注册为滑动。