以编程方式更改TabBatController.selectedIndex时的视图转换 [英] View transitions when TabBatController.selectedIndex changed programmatically

查看:160
本文介绍了以编程方式更改TabBatController.selectedIndex时的视图转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以编程方式更改 UITabBarController.selectedIndex 时,我很难使视图过渡动画化。



当我点击 TabBar 图标时,动画效果很好,但是当我更改 selectedIndex 来自 gestureRecognizer 动作。



TabBar控制器类的转换代码

  func tabBarController(_ tabBarController:UITabBarController,shouldSelect viewController:UIViewController)->布尔{
,如果CanChangeTab {
保护let fromView = tabBarController.selectedViewController!.view,让toView = viewController.view else {
返回false //确保您将此设置为false
}

if fromView!= toView {
if(tabBarController.prevIndex> tabBarController.selectedIndex){
UIView.transition(from:fromView,to:toView,持续时间: 0.3,选项:[。transitionFlipFromLeft],完成:无)
}否则{
UIView.transition(from:fromView,to:toView,duration:0.3,选项:[.transitionFlipFromRight],完成:nil )
}
}
返回true
}否则{
return false
}
}

手势识别器正在调用以下函数,而上面的代码未被调用:

  @objc func swiped(_手势:UISwipeGestureRecognizer){ 
if(CanChangeTab){
self.tabBarController?.prevIndex =(self.tabBarController?.selectedIndex)!
if pose.direction == .left {
if(self.tabBarController?.selectedIndex)! < 4 {//在此处设置总计选项卡
self.tabBarController?.selectedIndex + = 1
}
}否则if pose.direction == .right {
if(self。 tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex-= 1
}
}
}
}

我也看不到应该调用或重写什么来获得手势基动画的动画。

解决方案

好。我使用 ViewController幻灯片动画



找到了解决方案如马特所建议。



因此,使用扩展名+扩展名中的animateToTab函数并更改我的滑动方法,即可按预期工作。

  @objc func swiped(_手势:UISwipeGestureRecognizer){
if(CanChangeTab){
让thisTabController = self.tabBarController为! iBayTabController

thisTabController.prevIndex =(thisTabController.selectedIndex)如果手势方向==则为
。left {
if thisTabController.selectedIndex< 4 {//在此处设置总计选项卡
thisTabController.animateToTab(toIndex:thisTabController.selectedIndex + 1)
}
}否则if pose.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
thisTabController.animateToTab(toIndex:thisTabController.selectedIndex-1)
}
}
}
}


I have some troubles to get the views transitions animated when I change the UITabBarController.selectedIndex changed programmatically.

When I tap on a TabBar icon the animation works fine, but when I change the selectedIndex from a gestureRecognizer action.

The transition code at the TabBar controller class is the following:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if CanChangeTab {
        guard let fromView = tabBarController.selectedViewController!.view, let toView = viewController.view else {
            return false // Make sure you want this as false
        }

        if fromView != toView {
            if (tabBarController.prevIndex > tabBarController.selectedIndex) {
                UIView.transition(from: fromView, to: toView, duration: 0.3, options: [.transitionFlipFromLeft], completion: nil)
            } else {
                UIView.transition(from: fromView, to: toView, duration: 0.3, options: [.transitionFlipFromRight], completion: nil)
            }
        }
        return true
    } else {
        return false
    }
}

The gesture recogniser is calling the following function, from which the above code is not called:

@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
    if (CanChangeTab) {
        self.tabBarController?.prevIndex = (self.tabBarController?.selectedIndex)!
        if gesture.direction == .left {
            if (self.tabBarController?.selectedIndex)! < 4 { // set your total tabs here
                self.tabBarController?.selectedIndex += 1
            }
        } else if gesture.direction == .right {
            if (self.tabBarController?.selectedIndex)! > 0 {
                self.tabBarController?.selectedIndex -= 1
            }
        }
    }
}

I cannot see what should be called or overridden to get the animations for the gesture base change too.

解决方案

Ok. I have found the solution, using ViewController slide animation

as Matt proposed.

So using the extension + the animateToTab function in the extension and changing my swipe method it works just as expected.

   @objc func swiped(_ gesture: UISwipeGestureRecognizer) {
    if (CanChangeTab) {
        let thisTabController = self.tabBarController as! iBayTabController

        thisTabController.prevIndex = (thisTabController.selectedIndex)
        if gesture.direction == .left {
            if thisTabController.selectedIndex < 4 { // set your total tabs here
                thisTabController.animateToTab(toIndex: thisTabController.selectedIndex+1)
            }
        } else if gesture.direction == .right {
            if (self.tabBarController?.selectedIndex)! > 0 {
                thisTabController.animateToTab(toIndex: thisTabController.selectedIndex-1)
            }
        }
    }
}

这篇关于以编程方式更改TabBatController.selectedIndex时的视图转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆