首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell自定义子视图不接收焦点

UICollectionViewCell自定义子视图不接收焦点
EN

Stack Overflow用户
提问于 2020-06-27 02:35:12
回答 1查看 73关注 0票数 0

我创建了自定义Poster视图,以便可以在多个集合视图单元格中重用(就像TVUIKit中的TVPosterView一样)。我将其直接添加到具有所有所需约束的单元格内容视图中。

问题是,当单元格被聚焦时,此子视图不会接收焦点更新(didUpdateFocus..)所以我不能自定义它的聚焦/非聚焦约束等。

如果我将单元格的preferredFocusEnvironments指定为return [self.posterView] + super. preferredFocusEnvironments,UI将按预期运行,但未调用集合视图委托方法didSelect!

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2020-06-28 00:21:01

看起来didUpdateFocus并不是在关注的单元格及其系统设计的所有子视图上调用的。来自文档:

将焦点更新为新视图后,焦点引擎将在所有焦点环境上调用此方法,这些环境以升序顺序包含上一个焦点视图、下一个焦点视图或这两个视图。您应该重写此方法以更新应用程序的状态,以响应焦点的更改。使用提供的动画协调器以动画形式呈现与更新相关的视觉外观更改。有关动画协调器的详细信息,请参见UIFocusAnimationCoordinator。

注意:这意味着didUpdateFocus将首先在UICollectionViewCell上被调用,而不是在UIViewController子类上,按照升序的顺序。对于子视图,您需要手动注册将在通知更新中触发的customDidUpdateFocus方法。例如,要更新它的外观,我们可以使用通知(tvOS 11+),请参见下面的示例。

代码语言:javascript
复制
func customDidUpdateFocus(isFocused: Bool, with coordinator: UIFocusAnimationCoordinator) { /* Custom logic to customize appearance */ }

// Register observer
observerToken = NotificationCenter.default.addObserver(forName: UIFocusSystem.didUpdateNotification, object: nil, queue: .main) { [weak self] (note) in
        guard let self = self else { return }
        guard let context = note.userInfo?[UIFocusSystem.focusUpdateContextUserInfoKey] as? UIFocusUpdateContext else { return }
        guard let coordinator = note.userInfo?[UIFocusSystem.animationCoordinatorUserInfoKey] as? UIFocusAnimationCoordinator else { return }
        
        if let prev = context.previouslyFocusedView, self.isDescendant(of: prev) {
            self.didUpdateFocus(isFocused: false, with: coordinator)
        } else if let next = context.nextFocusedView, self.isDescendant(of: next) {
            self.didUpdateFocus(isFocused: true, with: coordinator)
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62600930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档