我不记得确切,但我知道有一个已知问题remembersLastFocusedIndexPath
,它没有按预期工作。
这是一种解决方法,尽管使用大量盐,因为它看起来确实有点hacky,并且它使用了覆盖preferredFocusedView
属性的常见(但可能不稳定)的方法。
private var viewToFocus: UIView?
override var preferredFocusView: UIView? {
get {
return self.viewToFocus
}
}
呈现时在本地保存最后一个单元格的 indexPathView A
View B
// [1] Saving and scrolling to the correct indexPath:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
...
collectionView.scrollToItemAtIndexPath(indexPath:, atScrollPosition:, animated:)
}
// [2] Create a dispatchTime using GCD before setting the cell at indexPath as the preferredFocusView:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
...
let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(CGFloat.min * CGFloat(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.viewToFocus = collectionView.cellForItemAtIndexPath(indexPath:)
// [3] Request an update
self.setNeedsFocusUpdate()
// [4] Force a focus update
self.updateFocusIfNeeded()
}
}
我们将这两种方法分成两者的原因viewWillAppear
是viewDidAppear
它消除了一些动画跳跃。如果其他人可以提出改进甚至替代解决方案的建议,我也会感兴趣!