Swift 如何向上滑动TabBar以在tvOS应用程序中隐藏它?
在我的tvOS应用程序中,我有一个带有3个ViewController的TabBarController。我要做的是在切换到下一个viewController时自动隐藏/更改选项卡栏的焦点 我在这里看到了一些帖子,建议在选项卡栏上更改alfa,但我希望有一个向上滑动的动画,就像在viewController中将焦点更改为某个对象时一样Swift 如何向上滑动TabBar以在tvOS应用程序中隐藏它?,swift,uitabbarcontroller,tabbar,tvos,Swift,Uitabbarcontroller,Tabbar,Tvos,在我的tvOS应用程序中,我有一个带有3个ViewController的TabBarController。我要做的是在切换到下一个viewController时自动隐藏/更改选项卡栏的焦点 我在这里看到了一些帖子,建议在选项卡栏上更改alfa,但我希望有一个向上滑动的动画,就像在viewController中将焦点更改为某个对象时一样 非常感谢您提供的任何帮助。下面描述的解决方案的基本思想是将UIAbbarController子类化,并有选择地使用super实现弱var preferredFoc
非常感谢您提供的任何帮助。下面描述的解决方案的基本思想是将
UIAbbarController
子类化,并有选择地使用super
实现弱var preferredFocusedView:UIView?{get}
或返回selectedViewController?.preferredFocusView
以及DidUpdateFocusInText(:withAnimationCoordinator:)
的实现,该实现设置一个NSTimer
来触发焦点更新,并设置一个控制preferredFocusView
实现的标志
更详细地说,子类UITabBarController
和重写didUpdateFocusInText(上下文:UIFocusUpdateContext,带动画协调器:UIFocusAnimationCoordinator)
。在您的实现中(确保调用super
实现),您可以检查上下文并确定tabBar
属性的子视图是nextFocusedView
还是previousFocusedView
(并且nextFocusedView
不是子视图)
如果选项卡栏正在获得焦点,您可以在隐藏选项卡栏之前创建一个NSTimer
,显示该选项卡栏的持续时间。如果选项卡栏在计时器触发前失去焦点,请使其无效。如果计时器触发,请调用setNeedsFocusUpdate()
,然后调用updateFocusIfNeeded()
要使其工作,您需要的最后一件事是在设置计时器时将标志设置为
true
。然后需要覆盖弱var preferredFocusedView:UIView?{get}
并调用super
实现,如果标志为false
且为true
则返回selectedViewController?。如Charles所说,首选聚焦视图
。。在派生的UITabBarController中类似于以下内容:
var focusOnChildVC : Bool = false {
didSet {
self.setNeedsFocusUpdate()
}
};
override weak var preferredFocusedView: UIView? {
get {
let v : UIView?;
let focused = UIScreen.mainScreen().focusedView
//A bit of a hack but seems to work for picking up whether the VC is active or not
if (focusOnChildVC && focused != nil) {
v = self.selectedViewController?.preferredFocusedView
} else {
//If we are focused on the main VC and then clear out of property as we're done with overriding the focus now
if (focusOnChildVC) {
focusOnChildVC = false
}
v = super.preferredFocusedView;
}
return v
}
}
您可以在UITabBarController子类中执行此操作:
final class TabBarViewController: UITabBarController {
private(set) var isTabBarHidden = false
func setTabBarHidden(_ isHidden: Bool, animated: Bool) {
guard isTabBarHidden != isHidden else {
return
}
var frame: CGRect
let alpha: CGFloat
if isHidden {
frame = tabBar.frame
frame.origin.y -= frame.height
alpha = 0
} else {
frame = tabBar.frame
frame.origin.y += frame.height
alpha = 1
}
let animations = {
self.tabBar.frame = frame
self.tabBar.alpha = alpha
}
if animated {
UIView.animate(withDuration: 0.3, animations: animations)
} else {
animations()
}
isTabBarHidden = isHidden
}
}
你可以试试这个,你能给我一段代码让我看得更清楚吗?我请求