【发布时间】:2020-06-12 11:12:42
【问题描述】:
我使用这个函数来动画标签栏的过渡
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let tabViewControllers = tabBarController.viewControllers, let toIndex = tabViewControllers.firstIndex(of: viewController) else {
return false
}
animateToTab(toIndex: toIndex)
return true
}
func animateToTab(toIndex: Int) {
guard let tabViewControllers = viewControllers,
let selectedVC = selectedViewController else { return }
guard let fromView = selectedVC.view,
let toView = tabViewControllers[toIndex].view,
let fromIndex = tabViewControllers.firstIndex(of: selectedVC),
fromIndex != toIndex else { return }
// Add the toView to the tab bar view
fromView.superview?.addSubview(toView)
// Position toView off screen (to the left/right of fromView)
let screenWidth = UIScreen.main.bounds.size.width
let scrollRight = toIndex > fromIndex
let offset = (scrollRight ? screenWidth : -screenWidth)
toView.center = CGPoint(x: fromView.center.x + offset, y: toView.center.y)
// Disable interaction during animation
view.isUserInteractionEnabled = false
UIView.animate(withDuration: 0.3,
delay: 0.0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .curveEaseOut,
animations: {
// Slide the views by -offset
fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y)
toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y)
}, completion: { finished in
// Remove the old view from the tabbar view.
fromView.removeFromSuperview()
self.selectedIndex = toIndex
self.view.isUserInteractionEnabled = true
})
}
当过渡发生时,控制器的顶部栏颜色会发生变化,并会导致令人讨厌的闪光效果。
请看视频
https://drive.google.com/file/d/15mlR9NfV_1C8gwi-4vfpV4CxpXbG5BUm/view
我怎样才能防止这种情况发生?
【问题讨论】:
-
你用什么做顶栏?它是一个导航栏还是别的什么?半透明属性是启用还是禁用?
-
@MihaiErős 导航栏样式为黑色半透明
-
禁用半透明并尝试一下,让我知道它是怎么回事。
-
但是对于这个项目来说,半透明的外观是必须的。有没有什么办法可以让它在不禁用半透明的情况下正常工作?
-
嗯,所以你想要一个带有那种 alpha 的黑色导航栏,对吧?但没有半透明带来的故障。
标签: swift animation uitabbarcontroller