在Swift中为具有0个单元格的tableView添加SwipeGesture的方法

huangapple go评论59阅读模式
英文:

How to add SwipeGesture to tableView with 0 cells in Swift

问题

我需要将左滑手势识别器添加到我的 CommentVC 中。我的 CommentVC 包括一个 tableView,所以如果在 CommentVC 中没有任何单元格,那么我就无法向左滑动。

我的代码: 使用这段代码,只有在该表格视图的区域中至少有一个单元格时才能进行左滑。但我希望在没有单元格的情况下也能左滑。我该如何实现呢?请指导我。

class CommentVC: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.left
        self.tableView.addGestureRecognizer(swipeRight)
    }

    @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.left:
                print("向左滑动")
                self.navigationController?.popViewController(animated: true)
            default:
                break
            }
        }
    }

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

以上是您的代码翻译部分。

英文:

I need to add a left swipe gesture recognizer to my CommentVC. My CommentVC includes a tableView, so if I have 0 cells in CommentVC then I am unable to swipe back.

My code: with this code I can swipe only if there at least one cell in that area of that tableview. But I need to swipe if there is no cell as well. How can I achieve that? Please guide me.

class CommentVC: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self    
    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizer.Direction.left
    self.tableView.addGestureRecognizer(swipeRight)
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizer.Direction.left:
            print("Swiped left")
            self.navigationController?.popViewController(animated: true)
        default:
            break
        }
    }
}

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

}

答案1

得分: 1

根据您的问题,我推测您的代码中的 touchView 是一个 UITableView

如果是这样,您可以简单地向表视图添加一个通用的背景视图,然后将相同的手势识别器添加到背景视图上。

// 假设 touchView 的类型是 UITableView,且其框架等于 self.view.bounds
self.touchView.backgroundView = UIView()
self.touchView.backgroundView?.addGestureRecognizer(swipeRight)
英文:

By your question, I suppose touchView in your code is a UITableView.

If so, you can simply add a generic background view to the table view, then add the same gesture recognizer to the background view.

    // assuming touchView is of type UITableView, and its frame is equal to self.view.bounds
    self.touchView.backgroundView = UIView()
    self.touchView.backgroundView?.addGestureRecognizer(swipeRight)

答案2

得分: 1

I just tried this and its completely worked. i have just changed one line:

我刚刚尝试了这个,它完全有效。我只改了一行:

self.tableView.addGestureRecognizer(swipeRight)

以上一行我已经用这一行替换了以上一行。然后能够在UITableView中进行无单元格和有单元格的滑动。我已将UISwipeGestureRecognizer添加到包含UITableView的父视图中:

self.view.addGestureRecognizer(swipeRight)

这是完整的代码:

这是完整的代码:

class CommentVC: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self    
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeRight)
    }

    @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.left:
                print("向左滑动")
                self.navigationController?.popViewController(animated: true)
            default:
                break
            }
        }
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self    
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeRight)
    }

    @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.left:
                print("向左滑动")
                self.navigationController?.popViewController(animated: true)
            default:
                break
            }
        }
    }

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

希望这有所帮助。

英文:

I just tried this and its completely worked. i have just changed one line

self.tableView.addGestureRecognizer(swipeRight)

above line i have replaced above line with this line. then able to swipe with no cells and with cells in tableview i have added a UISwipeGestureRecognizer to the superview that contains the UITableView

self.view.addGestureRecognizer(swipeRight)

this is total code:

class CommentVC: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.delegate = self    
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeRight)

}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
    switch swipeGesture.direction {
    case UISwipeGestureRecognizer.Direction.left:
        print("Swiped left")
        self.navigationController?.popViewController(animated: true)
    default:
        break
    }
}
}

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

}

huangapple
  • 本文由 发表于 2023年7月6日 21:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定