ios - wait until scrollToRowAtIndexPath is done in swift -
i have uitableview
more cells fit on screen. when notification data model want jump specific row , show basic animation.
my code is:
func animatebackgroundcolor(indexpath: nsindexpath) { dispatch_async(dispatch_get_main_queue()) { nslog("table should @ 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 selected row") } self.animatebackgroundcolor(nsindexpath(forrow: 0, insection: index)) } }
i hoped dispatch_sync part delay execution of animatebackgroundcolor
method until scrolling done. unfortunately not case animatebackgroundcolor
gets called when row not visible yet -> cellforrowatindexpath
returns nil
, animation won't happen. if no scrolling needed animation works without problem.
can tell how delay execution of animatebackgroundcolor
function until scrolling done?
thank , kind regards
delaying animation not seem solution since scrolltorowatindexpath
animation duration set based on distance current list item specified item. solve need execute animatebackgroudcolor after scrolltorowatindexpath
animation completed implementing scrollviewdidendscrollinganimation
uitableviewdelegate method. tricky part here indexpath @ tableview did scroll. possible workaround:
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 selected row") } } } func scrollviewdidendscrollinganimation(scrollview: uiscrollview) { self.animatebackgroundcolor(self.indexpath) indexpath = nil }
Comments
Post a Comment