gpt4 book ai didi

ios - 等到 scrollToRowAtIndexPath swift 完成

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:01 35 4
gpt4 key购买 nike

我有一个 UITableView,其中的单元格多于屏幕上显示的单元格。当我收到来 self 的数据模型的通知时,我想跳转到特定行并显示一个非常基本的动画。

我的代码是:

 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 -> cellForRowAtIndexPath 返回 nil 并且我的动画不会发生。如果不需要滚动,则动画可以正常工作。

谁能告诉我如何延迟执行我的 animateBackgroundColor 函数直到滚动完成?

非常感谢和亲切的问候

最佳答案

延迟动画似乎不是一个好的解决方案,因为 scrollToRowAtIndexPath 动画持续时间是根据当前列表项到指定项的距离设置的。要解决此问题,您需要在 scrollToRowAtIndexPath 动画完成后执行 animateBackgroudColor,方法是实现 scrollViewDidEndScrollingAnimation UITableViewDelegate 方法。这里棘手的部分是获取 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
}

关于ios - 等到 scrollToRowAtIndexPath swift 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641801/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com