tvOS:如何处理 TabBar 和 UICollectionView 之间的焦点变化并防止崩溃
tvOS: How to Handle Focus Change between TabBar and UICollectionView and Prevent Crashing
我通过 Xcode 的 tvOS "tabbed application" 选择创建了一个测试 tvOS 应用程序。在第一个选项卡视图控制器中放置一个带有一些简单文本单元格的 UIcollectionView。
应用程序运行时,可以从选项卡栏向下移动到接受焦点的 UICollectionView,并且可以毫无问题地在视图的项目之间移动。
但是,尝试从 UICollectionView 返回到标签栏会使应用程序崩溃并显示以下消息:
无法将类型 'UITabBarButton' (0x199b92828) 的值转换为 'tabtest.itemCell' (0x1000153c8)。
我认为这意味着焦点引擎只是 "seeing" 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
转换:当焦点从集合视图移动到选项卡栏时,context.nextFocusedView
将是 UITabBarButton
而不是集合视图细胞。老实说,我对 Swift 的了解还不够确定,但我认为如果你删除 !
那么你应该不会再看到这个崩溃了?
答案是意识到不需要 "if let cell . . ."。添加以下内容使其正常工作。
// 对于焦点引擎
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
}
我通过 Xcode 的 tvOS "tabbed application" 选择创建了一个测试 tvOS 应用程序。在第一个选项卡视图控制器中放置一个带有一些简单文本单元格的 UIcollectionView。
应用程序运行时,可以从选项卡栏向下移动到接受焦点的 UICollectionView,并且可以毫无问题地在视图的项目之间移动。
但是,尝试从 UICollectionView 返回到标签栏会使应用程序崩溃并显示以下消息:
无法将类型 'UITabBarButton' (0x199b92828) 的值转换为 'tabtest.itemCell' (0x1000153c8)。
我认为这意味着焦点引擎只是 "seeing" 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
转换:当焦点从集合视图移动到选项卡栏时,context.nextFocusedView
将是 UITabBarButton
而不是集合视图细胞。老实说,我对 Swift 的了解还不够确定,但我认为如果你删除 !
那么你应该不会再看到这个崩溃了?
答案是意识到不需要 "if let cell . . ."。添加以下内容使其正常工作。
// 对于焦点引擎
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
}