等待scrollToRowAtIndexPath快速完成

问题描述 投票:4回答:3

我的UITableView的单元格超出了屏幕显示的大小。当我从数据模型收到通知时,我想跳到特定行并显示一个非常基本的动画。

我的代码是:

 func animateBackgroundColor(indexPath: NSIndexPath) {        
    dispatch_async(dispatch_get_main_queue()) {
        NSLog("table should be at the right position")
        if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? BasicCardCell {
            var actColor = cell.backgroundColor
            self.manager.vibrate()
            UIView.animateWithDuration(0.2, animations: { cell.backgroundColor = UIColor.redColor() }, completion: {
                _ in

                UIView.animateWithDuration(0.2, animations: { cell.backgroundColor = actColor }, completion: { _ in
                        self.readNotificationCount--
                        if self.readNotificationCount >= 0 {
                            var legicCard = self.legicCards[indexPath.section]
                            legicCard.wasRead = false
                            self.reloadTableViewData()
                        } else {
                            self.animateBackgroundColor(indexPath)
                        }
                })
            })
        }
    }
}    

func cardWasRead(notification: NSNotification) {
    readNotificationCount++
    NSLog("\(readNotificationCount)")

        if let userInfo = notification.userInfo as? [String : AnyObject], let index = userInfo["Index"] as? Int {

            dispatch_sync(dispatch_get_main_queue()){
                self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: index), atScrollPosition: .None, animated: true)

                self.tableView.layoutIfNeeded()
                NSLog("table should scroll to selected row")
            }
            self.animateBackgroundColor(NSIndexPath(forRow: 0, inSection: index))
        }
}

我希望dispatch_sync部分将延迟我的animateBackgroundColor方法的执行,直到完成滚动为止。不幸的是,情况并非如此,因此当该行尚不可见时会调用animateBackgroundColor->cellForRowAtIndexPathreturns nil并且我的动画不会发生。如果不需要滚动,则动画可以正常工作。

谁能告诉我如何在滚动完成之前延迟执行animateBackgroundColor函数?

非常感谢您

ios uitableview swift
3个回答
4
投票

延迟动画似乎不是一个好的解决方案,因为scrollToRowAtIndexPath动画持续时间是根据当前列表项到指定项的距离设置的。为了解决这个问题,您需要通过实现scrollToRowAtIndexPath UITableViewDelegate方法来在scrollViewDidEndScrollingAnimation动画完成后执行animateBackgroudColor。这里最棘手的部分是获取tableview滚动的indexPath。可能的解决方法:

var indexPath:NSIndexpath?

func cardWasRead(notification: NSNotification) {
readNotificationCount++
NSLog("\(readNotificationCount)")

    if let userInfo = notification.userInfo as? [String : AnyObject], let index = userInfo["Index"] as? Int{

        dispatch_sync(dispatch_get_main_queue()){

            self.indexPath = NSIndexPath(forRow: 0, inSection: index)
            self.tableView.scrollToRowAtIndexPath(self.indexPath, atScrollPosition: .None, animated: true)

            self.tableView.layoutIfNeeded()

            NSLog("table should scroll to selected row")
        }

    }

}

func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
    self.animateBackgroundColor(self.indexPath)
    indexPath = nil
}

0
投票

这是我的解决方法

1)创建一个.swift文件,并将下面的代码复制到其中:

typealias SwagScrollCallback = (_ finish: Bool) -> Void 

class UICollectionViewBase: NSObject, UICollectionViewDelegate {
    static var shared = UICollectionViewBase()

    var tempDelegate: UIScrollViewDelegate?
    var callback: SwagScrollCallback?

    func startCheckScrollAnimation(scroll: UIScrollView, callback: SwagScrollCallback?){
        if let dele = scroll.delegate {
            self.tempDelegate = dele
        }
        self.callback = callback
        scroll.delegate = self
    }
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        callback?(true)
        if let dele = self.tempDelegate {
            scrollView.delegate = dele
            tempDelegate = nil
        }
    }
}

extension UICollectionView {

    func scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, _ callback: SwagScrollCallback?){
        UICollectionViewBase.shared.startCheckScrollAnimation(scroll: self, callback: callback)
        self.scrollToItem(at: indexPath, at: scrollPosition, animated: true)
    }

}

2)示例:

@IBAction func onBtnShow(){

        let index = IndexPath.init(item: 58, section: 0)
        self.clv.scrollToItem(at: index, at: .centeredVertically) { [weak self] (finish) in

            guard let `self` = self else { return }

            // Change color temporarily
            if let cell = self.clv.cellForItem(at: index) as? MyCell {
                cell.backgroundColor = .yellow
                cell.lbl.textColor = .red
            }


            // Reset
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                self.clv.reloadData()
            }
        }
    }

3)我的github代码示例:github here


-2
投票

我有类似的问题。我只是这样做。

UIView.animateWithDuration(0.2,delay:0.0,options: nil,animations:{
          self.tableView.scrollToRowAtIndexPath(self.indexPath!, atScrollPosition: .Middle, animated: true)},
                        completion: { finished in UIView.animateWithDuration(0.5, animations:{
                            self.animateBackgroundColor(self.indexPath)})})}
© www.soinside.com 2019 - 2024. All rights reserved.