要在ViewControllers之间改变segue的速度,您可以使用以下方法:
创建一个自定义segue类,并在其中设置动画速度。例如:
import UIKit
class CustomSegue: UIStoryboardSegue {
override func perform() {
let sourceViewController = self.source
let destinationViewController = self.destination
sourceViewController.present(destinationViewController, animated: true, completion: nil)
// 设置动画速度
let transitionCoordinator = destinationViewController.transitionCoordinator
transitionCoordinator?.animate(alongsideTransition: { _ in
// 在这里设置动画速度
}, completion: nil)
}
}
然后在storyboard中为segue设置自定义类:
class ViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let customSegue = segue as? CustomSegue {
// 设置动画速度
customSegue.animationDuration = 0.5
}
}
}
UIViewControllerAnimatedTransitioning
协议:创建一个遵循UIViewControllerAnimatedTransitioning
协议的类,并在其中设置动画速度。例如:
import UIKit
class CustomTransition: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5 // 设置动画速度
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// 在这里实现动画
}
}
然后在segue之前设置转场代理:
class ViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationViewController = segue.destination
destinationViewController.transitioningDelegate = self
}
}
extension ViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomTransition()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomTransition()
}
}
这样,您就可以在ViewControllers之间改变segue的速度了。
领取专属 10元无门槛券
手把手带您无忧上云