gpt4 book ai didi

swift - ViewController 滑动动画

转载 作者:IT王子 更新时间:2023-10-29 05:41:25 24 4
gpt4 key购买 nike

我想创建一个动画,例如 iOS 应用 facebook at tabswitch[1]。我已经尝试开发某种动画,出现的问题是旧 View Controller 直接在开关上变得不可见,而不是在新 Controller 快速滑入时慢慢淡出。

我发现了这个 SO 问题 How to animate Tab bar tab switch with a CrossDissolve slide transition?但是正确标记的解决方案对我来说并不适用(它不是幻灯片,它是淡入淡出的过渡)。我还想得到的是向左或向右滑动以切换选项卡的功能。就像在旧版 Facebook 上一样。

到目前为止我得到的是:

extension TabBarController: UITabBarControllerDelegate  {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let fromView = selectedViewController?.view,
let toView = viewController.view else { return false }
if fromView != toView {
toView.transform = CGAffineTransform(translationX: -90, y: 0)
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
toView.transform = CGAffineTransform(translationX: 0, y: 0)
})
}; return true
}
}

class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}

如何解决这个问题?


[1]我非常想从 Facebook 应用程序添加 gif。问题是我不想审查视频,只是想透露太多我的数据。 (即使 fb 已经有了它们)。同样在 youtube 上我没有找到合适的录音。请在 iOS 的 fb 应用程序中自己尝试。

最佳答案

您可以使用以下思路:https://samwize.com/2016/04/27/making-tab-bar-slide-when-selected/

此外,这里是更新到 Swift 4.1 的代码,我还删除了强制展开:

import UIKit

class MyTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

delegate = self
}
}

extension MyTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let tabViewControllers = tabBarController.viewControllers, let toIndex = tabViewControllers.index(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.index(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
})
}
}

因此,您需要子类化 UITabBarController 并且您还必须编写动画部分,您可以调整动画选项(延迟、持续时间等)。

希望对你有帮助,干杯!

enter image description here

关于swift - ViewController 滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51482618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com