我正在尝试从一个ViewController到另一个ViewController进行自定义转换,但由于某种原因我似乎无法正确使用它。 我想要" fromView"淡出和" ToView"淡出印度,我没有' 我认为这会很难 - 但显然这并不容易。
现在我使用NSObject声明转换函数,并从" prepare(for segue"在我的ViewController中)调用它。但是出了点问题。
我的自定义转换:
class FadeAnimator: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
let duration = 1
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return TimeInterval(duration)
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
return
}
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
return
}
let container = transitionContext.containerView
container.addSubview(fromView)
container.addSubview(toView)
UIView.animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {
UIView.animate(withDuration: 2, animations: {
fromView.alpha = 0
}, completion: { (true) in
toView.alpha = 1
})
}) { (succes) in
transitionContext.completeTransition(succes)
}
}
我的ViewController.swift:
let fadeAnimater = FadeAnimator()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
destination.transitioningDelegate = FadeAnimator() as! UIViewControllerTransitioningDelegate
}