swift 如何在tvOS中获得聚焦视图帧大小?

u0sqgete  于 2023-05-16  发布在  Swift
关注(0)|答案(2)|浏览(128)

如何在tvOS中获取聚焦视图的大小?
我需要它的大小来将标签从视图中移动到适合焦点框架的新位置。

hwazgwia

hwazgwia1#

我想你要找的是UIImageView提供的focusedFrameGuide
使用imageview.focusedFrameGuide.layoutFrame可以确定如何重新定位标签。

myzjeezk

myzjeezk2#

使用“自动布局”需要设置两个约束:

focusedConstraint = label.topAnchor.constraint(equalTo: imageView.focusedFrameGuide.bottomAnchor)
unfocusedConstraint = label.topAnchor.constraint(equalTo: imageView.bottomAnchor)

并在didUpdateFocus上激活/停用:

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        super.didUpdateFocus(in: context, with: coordinator)

        if context.nextFocusedView == self {
            unfocusedConstraint?.isActive = false
            focusedConstraint?.isActive = true
        } else if context.previouslyFocusedView == self {
            focusedConstraint?.isActive = false
            unfocusedConstraint?.isActive = true
        }

        coordinator.addCoordinatedAnimations({
            self.layoutIfNeeded()
        }, completion: nil)
    }

相关问题