簡體   English   中英

在表格視圖中同時使用輕擊手勢和長按

[英]Using tap gesture and long press at the same time in Table View

我正在建立表格視圖,似乎無法同時使用常規水龍頭和長按來工作。

我已將此代碼放置在viewDidLoad中:

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
myTableView.addGestureRecognizer(longPress)

這段代碼是我的手勢識別器:

@objc func handleLongPress(sender: UILongPressGestureRecognizer){
    if UILongPressGestureRecognizer.state == UIGestureRecognizer.State.began {

        let touchPoint = UILongPressGestureRecognizer.location(in: self.myTableView)
        if let indexPath = self.myTableView.indexPathForRowAtPoint(touchPoint) {
            print(indexPath.row)
        }
    }
}

我在Stack Overflow上找到了此代碼,但是我認為它不是Swift 4的最新版本,因為在構建失敗的情況下,我什至無法運行它。

UILongPressGestureRecognizer.state應該是sender.stateUILongPressGesutreRecognizer.location應該是sender.location 此外, indexPathForRowAtPoint()的簽名已更新為indexPathForRow(at:)

正確的代碼:

@objc func handleLongPress(sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        let touchPoint = sender.location(in: self.myTableView)
        if let indexPath = self.myTableView.indexPathForRow(at:touchPoint) {
            print(indexPath.row)
        }
    }
}

UILongPressGestureRecognizer是一個類名稱,您需要調用該類實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM