How to make UITableview scroll when dragged cell reached at the top or bottom?

Viewed 155

Basically, my goal is

  1. To create two table views side by side and by scrolling one other tableview should also scroll.
  2. One of the tableview should possess a drag and drop cell functionally.
  3. When we drag cell and it reaches the top or bottom other table view content must be scrolled to (Both table views together) I have assembled this code that worked for the first two points but I am unable to achieve the 3rd point.
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var leftTableView: UITableView!
    @IBOutlet weak var rightTableView: UITableView!
    
    var dragInitialIndexPath: IndexPath?
    var dragCellSnapshot: UIView?
    
    var arr1 = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R"]
    var arr2 = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"]
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let longpress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(gestureRecognizer:)))
        self.rightTableView.addGestureRecognizer(longpress)

    }
    
    @objc func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {

        let longpress = gestureRecognizer as! UILongPressGestureRecognizer
        let state = longpress.state
        let locationInView = longpress.location(in: self.rightTableView)
        let indexPath = self.rightTableView.indexPathForRow(at: locationInView)
        switch state {
        case .began:
            if indexPath != nil {
                Path.initialIndexPath = indexPath
                let cell = self.rightTableView.cellForRow(at: indexPath!) as! SecondTableViewCell
                My.cellSnapShot = snapshopOfCell(inputView: cell)
                var center = cell.center
                My.cellSnapShot?.center = center
                My.cellSnapShot?.alpha = 0.0
                self.rightTableView.addSubview(My.cellSnapShot!)

                UIView.animate(withDuration: 0.25, animations: {
                    center.y = locationInView.y
                    My.cellSnapShot?.center = center
                    My.cellSnapShot?.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
                    My.cellSnapShot?.alpha = 0.98
                    cell.alpha = 0.0
                }, completion: { (finished) -> Void in
                    if finished {
                        cell.isHidden = true
                    }
                })
            }

        case .changed:
            var center = My.cellSnapShot?.center
            center?.y = locationInView.y
            My.cellSnapShot?.center = center!
            
            if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {

                self.arr2.swapAt((indexPath?.row)!, (Path.initialIndexPath?.row)!)
                self.rightTableView.moveRow(at: Path.initialIndexPath!, to: indexPath!)
                Path.initialIndexPath = indexPath
            }

        default:
            if let cell = self.rightTableView.cellForRow(at: Path.initialIndexPath!) as? SecondTableViewCell{
                cell.isHidden = false
                cell.alpha = 0.0
                UIView.animate(withDuration: 0.25, animations: {
                    My.cellSnapShot?.center = cell.center
                    My.cellSnapShot?.transform = .identity
                    My.cellSnapShot?.alpha = 0.0
                    cell.alpha = 1.0
                }, completion: { (finished) -> Void in
                    if finished {
                        Path.initialIndexPath = nil
                        My.cellSnapShot?.removeFromSuperview()
                        My.cellSnapShot = nil
                    }
                })
            }
        }
    }

    func snapshopOfCell(inputView: UIView) -> UIView {

        UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
        inputView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        let cellSnapshot : UIView = UIImageView(image: image)
        cellSnapshot.layer.masksToBounds = false
        cellSnapshot.layer.cornerRadius = 0.0
        cellSnapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
        cellSnapshot.layer.shadowRadius = 5.0
        cellSnapshot.layer.shadowOpacity = 0.4
        return cellSnapshot
    }

    struct My {
        static var cellSnapShot: UIView? = nil
    }

    struct Path {
        static var initialIndexPath: IndexPath? = nil
    }

}

extension ViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr1.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if tableView == leftTableView{
            let cell = leftTableView.dequeueReusableCell(withIdentifier: "cell1") as! FirstTableViewCell
            cell.firstLabel.text = arr1[indexPath.row]
            return cell
            
        }else{
            let cell = rightTableView.dequeueReusableCell(withIdentifier: "cell2") as! SecondTableViewCell
            cell.secondLabel.text = arr2[indexPath.row]
            return cell
        }
        
    }
}

extension ViewController: UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == leftTableView {
            rightTableView.contentOffset = scrollView.contentOffset
            rightTableView.contentSize.height = scrollView.contentSize.height
            
        } else {
            leftTableView.contentOffset = scrollView.contentOffset
            leftTableView.contentSize.height = scrollView.contentSize.height
        }
    }
}

I am a beginner in swift haven't done much code. If someone guides me on how I can achieve the 3rd point it will be a great favor. If someone suggests some other way to achieve these goals then most welcome.

0 Answers
Related