当我更改以编程方式更改的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
}
}
}
}
我无法看到应该调用或覆盖的内容以获取手势基础更改的动画。
问题是你正在做的不是如何做标签栏控制器动画。您必须编写正式结构的自定义动画过渡。
这意味着:
animationControllerForTransitionFrom
以返回UIViewControllerAnimatedTransitioning对象,interactionControllerFor
返回一个UIViewControllerInteractiveTransitioning对象。startInteractiveTransition
,interruptibleAnimator(using:)
,transitionDuration(using:)
,animateTransition(using:)
和animationEnded
来执行动画。然后,手势识别器将能够通过设置selectedIndex
来触发动画,并且能够通过提供的UIViewControllerContextTransitioning对象和UIViewPropertyAnimator来跟踪和更新动画。
好。我找到了解决方案,使用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)
}
}
}
}