Vladimir Zivanov
Vladimir Zivanov

Reputation: 380

UITableViewCell scroll detecting

I have pangesture recognizer in custom UITableViewCell and I want to disable it during table view scrolling. Is it possible to detect in custom UITableViewCell is table view is scrolling?

Upvotes: 2

Views: 676

Answers (2)

Kishan Suthar
Kishan Suthar

Reputation: 658

Use this extension for detect specific tableview scrolling in iOS Swift

extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == tableName {
            // write logic for tableview disble scrolling
        }
    }
}

Upvotes: 1

Bhaumik
Bhaumik

Reputation: 1238

As @jarvis12 mentioned in comment, UITableView inherits from UIScrollView and you can take advantage of its delegate methods.

  1. Add a global bool variable which will act as a flag to check current state of scrolling.

    var isScrolling = false
    
  2. Add two UIScrollView delegate methods and update isScrolling variable as below:

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.isScrolling = true
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        self.isScrolling = false
    }
    
  3. In your UITableViewCell simply add following if condition:

    if isScrolling {
        //disable pan gesture
    }
    else {
        //enable pan gesture
    }
    

Upvotes: 1

Related Questions