TabBatController.selectedIndex以编程方式更改时查看转换

问题描述 投票:0回答:2

当我更改以编程方式更改的UITabBarController.selectedIndex时,我有一些麻烦让视图过渡动画。

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

TabBar控制器类的转换代码如下:

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
    }
}

手势识别器调用以下函数,不调用上述代码:

@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
            }
        }
    }
}

我无法看到应该调用或覆盖的内容以获取手势基础更改的动画。

swift animation uitabbarcontroller selectedindex
2个回答
0
投票

问题是你正在做的不是如何做标签栏控制器动画。您必须编写正式结构的自定义动画过渡。

这意味着:

  • 您的标签栏控制器有一个委托实现animationControllerForTransitionFrom以返回UIViewControllerAnimatedTransitioning对象,interactionControllerFor返回一个UIViewControllerInteractiveTransitioning对象。
  • 这些对象通过UIViewPropertyAnimator实现startInteractiveTransitioninterruptibleAnimator(using:)transitionDuration(using:)animateTransition(using:)animationEnded来执行动画。

然后,手势识别器将能够通过设置selectedIndex来触发动画,并且能够通过提供的UIViewControllerContextTransitioning对象和UIViewPropertyAnimator来跟踪和更新动画。


0
投票

好。我找到了解决方案,使用ViewController slide animation

正如马特提出的那样。

因此,使用扩展中的扩展+ animateToTab函数并更改我的滑动方法,它可以正常工作。

   @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)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.