gpt4 book ai didi

ios - tvOS - 嵌套 CollectionView 未正确聚焦

转载 作者:行者123 更新时间:2023-11-29 05:31:17 24 4
gpt4 key购买 nike

我有一个嵌套 Collection View (外部垂直 Collection View ,内部水平 Collection View 作为其单元格)。焦点正确向下移动,但由于某种原因,它没有向上/向右移动。

我根据Apple的示例代码关闭了外部 Collection View 的焦点。

管理内部 Collection View 的外部 Collection View 自定义单元格的代码:

func indexPathForPreferredFocusedView(in collectionView: 
UICollectionView) -> IndexPath? {
return IndexPath(item: 0, section: 0)
}

override var canBecomeFocused: Bool {
return true
}

override var preferredFocusEnvironments: [UIFocusEnvironment] {
if let v = UIScreen.main.focusedItem as? UICollectionViewCell {
if collectionView.indexPath(for: v) != nil {
return super.preferredFocusEnvironments
}
}
return [collectionView]
}

内部 Collection View 自定义单元格的代码(更新焦点):

class CustomCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var posterView: UIImageView!

override var canBecomeFocused: Bool {
return true
}

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if context.previouslyFocusedView === self {
UIView.animate(withDuration: 0.1) {
context.previouslyFocusedView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}
}


if context.nextFocusedView === self {
UIView.animate(withDuration: 0.1) {
context.nextFocusedView?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}
}
}
}

焦点应正确地向上、向下、向左或向右移动

最佳答案

问题在于我创建 xib 文件的方式(我直接为 CustomCollectionViewCell 创建了接口(interface)类型为“View”的新 xib)。使用xib创建Cocoa Touch类并删除不需要的cocoa touch类。另一种解决方案是使用 Storyboard并从中加载 CustomCollectionViewCell。

关于ios - tvOS - 嵌套 CollectionView 未正确聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57530018/

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