Использование жеста панорамирования для выбора нескольких ячеек таблицы - PullRequest
0 голосов
/ 05 декабря 2018

Я пытаюсь реализовать жест панорамирования в табличном представлении, чтобы выбрать несколько ячеек при их панорамировании по

Проблема, с которой я сталкиваюсь, заключается в том, чтобы заставить табличное представление перейти к следующей ячейке, когда пользовательперемещается по последней видимой ячейке таблицы или прокручивает до предыдущей ячейки, когда пользователь перемещается по первой видимой ячейке

В настоящее время, когда панорамирование достигает последней видимой или первой видимой строки в таблице, она анимируетследующий ряд, но только для первых 2 или 3 строк, а затем он перестает прокручиваться

После небольшого копания кажется, что это происходит из-за того, что panGesture не вызывается из-за того, что палец пользователяканцелярские товары на краю стола.

Как мне прокрутить стол, как только сковорода достигнет верхнего или нижнего края стола?

Вот мой текущий код:

import UIKit

class MyView: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {


    var lastSelectedCell = IndexPath()
    var gesturePan: UIPanGestureRecognizer!

    var totalRows = 100


    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        //setup table gestures
        addGesturesToTable()

    }


}




extension MyView{

//MARK: - Tableview code
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return totalRows
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let lab = cell.viewWithTag(1) as! UILabel
    lab.text = "\(indexPath.row)"
    return cell
}


}


extension MyView{

//MARK: - Gesture code
func addGesturesToTable() {
    tableView.canCancelContentTouches = false
    tableView.allowsMultipleSelection = true

    //add long press gesture to initiate pan
    let gestureLongPress = UILongPressGestureRecognizer(target: self, action: #selector(enablePan))
    gestureLongPress.minimumPressDuration = 0.25
    gestureLongPress.delaysTouchesBegan = true
    gestureLongPress.delegate = self
    tableView.addGestureRecognizer(gestureLongPress)

    //add pan gesture to select cells on drag
    gesturePan = UIPanGestureRecognizer(target: self, action: #selector(userPanned(_:)))
    gesturePan.isEnabled = false
    gesturePan.delegate = self
    tableView.addGestureRecognizer(gesturePan)
}



//LongPress gesture(to enable pan)
@objc func enablePan() {
    gesturePan.isEnabled = true
}


//Pan gesture
@objc func userPanned(_ panGesture: UIPanGestureRecognizer) {

    //if starting to pan
    if panGesture.state == .began {
        tableView?.isUserInteractionEnabled = false
        tableView?.isScrollEnabled = false


    //if pan position is changed
    }else if panGesture.state == .changed {

        //get indexPath at pan location
        let location: CGPoint = panGesture.location(in: tableView)
        if let indexPath: IndexPath = tableView?.indexPathForRow(at: location) {

            //if the index path.row is the first or last row of the data set
            if(indexPath.row >= totalRows - 1 || indexPath.row <= 0){
               print("data start/end reached")

            //otherwise
            }else{

                //get pan direction
                var direction: String!
                let velocity = panGesture.velocity(in: tableView)
                if(velocity.y > 0){
                    direction = "Down"
                }else{
                    direction = "Up"
                }

                //if it's a new cell
                if indexPath != lastSelectedCell {

                    //select/deselect it
                    self.selectPannedCell(indexPath, selected: true, location: location, panDirection: direction)

                    //update last selected cell
                    lastSelectedCell = indexPath
                }
            }
        }

    //if pan gesture is ending
    }else if panGesture.state == .ended {

        //re-enable tableview interaction
        tableView.isScrollEnabled = true
        tableView.isUserInteractionEnabled = true

        //disable pan gesture
        panGesture.isEnabled = false
    }

}








//select/deselect the panned over cell
func selectPannedCell(_ indexPath: IndexPath, selected: Bool, location: CGPoint, panDirection: String) {
    if let cell = tableView.cellForRow(at: indexPath) {

        //if cell is already selected
        if cell.isSelected {

            //deselect row
            tableView.deselectRow(at: indexPath, animated: true)


            //if panning down
            if(panDirection == "Down"){

                //if it's the last cell
                if(indexPath >= tableView.indexPathsForVisibleRows![(tableView.indexPathsForVisibleRows?.count)!-1]){

                    //scroll to the next cell after the last visible
                    let scrollToIndex = IndexPath(row: indexPath.row + 1, section:0)
                    tableView.scrollToRow(at: scrollToIndex, at:
                        UITableViewScrollPosition.bottom, animated: true)
                }

                //if panning up
            }else{

                //if it's the first cell
                if(indexPath <= tableView.indexPathsForVisibleRows![0]){

                    //scroll to the previous cell before first visible
                    let firstIndex = IndexPath(row: indexPath.row - 1, section:0)
                    tableView.scrollToRow(at: firstIndex, at:
                        UITableViewScrollPosition.top, animated: true)
                }
            }


        } else {


            //select row
            tableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)

            //if panning down
            if(panDirection == "Down"){


                //if it's the last cell
                if(indexPath >= tableView.indexPathsForVisibleRows![(tableView.indexPathsForVisibleRows?.count)!-1]){

                    //scroll to the next cell after the last visible
                    let lastIndex = IndexPath(row: indexPath.row + 1, section:0)
                    tableView.scrollToRow(at: lastIndex, at:
                        UITableViewScrollPosition.bottom, animated: true)

                }

            }else{

                //if it's the first cell
                if(indexPath <= tableView.indexPathsForVisibleRows![0]){

                    //scroll to the previous cell before first visible
                    let firstIndex = IndexPath(row: indexPath.row - 1, section:0)
                    tableView.scrollToRow(at: firstIndex, at:
                        UITableViewScrollPosition.top, animated: true)

                }
            }

        }

    }
}


}

Заранее спасибо

1 Ответ

0 голосов
/ 05 декабря 2018

Есть быстрый пример того, как это может быть реализовано в вашем случае, я думаю, вы можете легко приспособить его для своих нужд.

var continueScrollToBottom: Bool = false
var continueScrollToTop: Bool = false

@objc func updatePanGesture(_ sender: UIPanGestureRecognizer) {
    switch sender.state {
    case .changed:

        continueScrollToBottom = false
        continueScrollToTop = false

        // If you reach to the top make scroll constant
        // You can define there another condition which more suitable for you
        if sender.location(in: self.view).y < 200 {
            continueScrollToTop = true
            continueScrollToBottom = false
            self.startScrollToTop()
            return
        }

        // If you reach to the bottom make scroll constant
        // You can define there another condition which more suitable for you
        if sender.location(in: self.view).y > 350 {
            continueScrollToBottom = true
            continueScrollToTop = false
            self.startScrollToBottom()
            return
        }

    case .ended:
        continueScrollToBottom = false
        continueScrollToTop = false

    case .cancelled:
        continueScrollToBottom = false
        continueScrollToTop = false

    default:
        break
    }
}

func startScrollToBottom() {
    if continueScrollToBottom {
        if let last = tableView.indexPathsForVisibleRows?.last, last.row+1 < countOfRows  {
            let showNext = IndexPath(row: last.row+1, section: last.section)
            tableView.scrollToRow(at: showNext, at: .bottom, animated: true)
            DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
                self.startScrollToBottom()
            }
        }
    }
}

func startScrollToTop() {
    if continueScrollToTop {
        if let first = tableView.indexPathsForVisibleRows?.first, first.row-1 > 0  {
            let showNext = IndexPath(row: first.row-1, section: first.section)
            tableView.scrollToRow(at: showNext, at: .top, animated: true)
            DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
                self.startScrollToTop()
            }
        }
    }
}
...