滑动动作延迟

时间:2019-02-03 22:06:40

标签: swift latency uiswipegesturerecognizer

我已向我的应用程序添加了滑动操作。我有5个代表星期几的按钮。当我单击一个按钮时,它会更改表视图的数组和其他内容。 滑动动作的效果完全相同,效果很好,但有时滑动后会冻结,几秒钟后又可以重新起作用。是等待时间。

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

您可以看到滑动动作可以执行相同操作,但有时会冻结,如果需要,请自己进行测试,问题出在Horaire视图。 https://github.com/CedricLnx/School-Companion

1 个答案:

答案 0 :(得分:0)

将Horaire类设置为UIGestureRecognizerDelegate的子类。

class Horaire: ViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate {

将每个Swipe的代表设置为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

添加UIGestureRecognizerDelegate函数。

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

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