[英]tvOS: How to Handle Focus Change between TabBar and UICollectionView and Prevent Crashing
我通過Xcode的tvOS“選項卡式應用程序”選擇創建了一個測試tvOS應用程序。 在第一個選項卡視圖控制器中放置一個帶有一些簡單文本單元格的UIcollectionView。
當應用程序運行時,可以從標簽欄向下移動到UICollectionView,它接受焦點,並且可以在視圖的項目之間移動而不會出現問題。
但是,嘗試從UICollectionView向上移回標簽欄會使應用程序崩潰,並顯示以下消息:
無法將'UITabBarButton'(0x199b92828)類型的值轉換為'tabtest.itemCell'(0x1000153c8)。
我認為這意味着焦點引擎只“看到”了collectionView,並且我必須添加代碼來處理跳轉回UITabBarButton。
我已經搜索了關於如何處理焦點變化的信息,並且沒有找到具體的信息。 作為iOS / tvOS開發的新手,我可能錯過了一些明顯的東西。
有人可以解釋如何管理標簽欄和UICollectionView之間的焦點?
這是我用來處理UICollectionView焦點的代碼:
// For Focus Engine
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
}
func collectionView(collectionView: UICollectionView, shouldUpdateFocusInContext context: UICollectionViewFocusUpdateContext) -> Bool {
if let cell: UICollectionViewCell = context.nextFocusedView as! itemCell{
_ = self.collectionView.indexPathForCell(cell)
}
return true
}
您無需添加任何焦點代碼即可在此處工作。 您在控制台中看到的錯誤消息是因為您as! itemCell
as! itemCell
cast:當焦點從集合視圖移動到標簽欄時, context.nextFocusedView
將是UITabBarButton
而不是集合視圖單元格。 老實說我不太了解Swift肯定,但我想如果你刪除了!
那么你應該停止看到這次崩潰?
答案是要意識到“如果讓細胞......” 不需要。 添加以下內容使其正常工作。
//對於Focus Engine
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
}
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func collectionView(collectionView: UICollectionView, shouldUpdateFocusInContext context: UICollectionViewFocusUpdateContext) -> Bool {
return true
}
func collectionView(collectionView: UICollectionView, shouldSlectItemAtIndexPath context: UICollectionViewFocusUpdateContext) -> Bool {
return true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.