iOS 8.3 - 交互式转换忽略 UIView 动画完成块

swiftIOS

1个回答

写回答

18614070975

2025-06-25 09:45

+ 关注

IOS
IOS

IOS 8.3 - 交互式转换忽略 UIView 动画完成块?

IOS 8.3 及更高版本中,我们可以使用交互式转换来创建自定义的转场动画。这使得我们能够以一种更灵活的方式管理视图控制器之间的过渡效果。然而,有时候我们可能会遇到一个问题,即在进行交互式转换时 UIView 动画的完成块被忽略。本文将探讨这个问题,并提供解决方案。

问题描述

在进行交互式转换时,我们通常会使用 UIPercentDrivenInteractiveTransition 类来管理动画的进度,并使用 UIViewPropertyAnimator 类创建动画。在动画完成时,我们可以通过设置动画的 completion block 来执行一些额外的操作。然而,在 IOS 8.3 及更高版本中,当进行交互式转换时,UIView 动画的完成块会被忽略,导致我们无法在动画完成时执行自定义的代码。

解决方案

要解决这个问题,我们需要通过实现 UIViewControllerTransitioningDelegate 协议的方法来自定义转场动画。我们可以创建一个遵循 UIViewControllerAnimatedTransitioning 协议的转场动画对象,并在其中实现动画的过程。这样,我们就可以在动画完成时执行自定义的代码。

下面是一个示例代码,演示了如何解决这个问题:

Swift

class CustomTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

return 0.5

}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

let contAInerView = transitionContext.contAInerView

let fromViewController = transitionContext.viewController(forKey: .from)!

let toViewController = transitionContext.viewController(forKey: .to)!

contAInerView.addSubview(toViewController.view)

UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

// 在这里执行你的自定义动画

}, completion: { _ in

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

// 在这里执行你想要在动画完成时执行的代码

})

}

}

class CustomTransitionDelegate: NSObject, UIViewControllerTransitioningDelegate {

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

return CustomTransitionAnimator()

}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

return CustomTransitionAnimator()

}

}

class ViewController: UIViewController {

let customTransitionDelegate = CustomTransitionDelegate()

// 在需要进行转场动画的地方调用这个方法

func presentNextViewController() {

let nextViewController = NextViewController()

nextViewController.transitioningDelegate = customTransitionDelegate

nextViewController.modalPresentationStyle = .custom

present(nextViewController, animated: true, completion: nil)

}

}

class NextViewController: UIViewController {

// 在这里添加你的视图控件和相关的代码

}

在上面的示例代码中,我们创建了一个自定义的转场动画对象 CustomTransitionAnimator,并实现了 UIViewControllerAnimatedTransitioning 协议的方法。在 animateTransition(using:) 方法中,我们可以执行自定义的动画,并在动画完成时调用 transitionContext.completeTransition() 来结束转场动画。

然后,我们创建了一个自定义的转场代理对象 CustomTransitionDelegate,并实现了 UIViewControllerTransitioningDelegate 协议的方法。在 animationController(forPresented:presenting:source:) 和 animationController(forDismissed:) 方法中,我们返回了我们自定义的转场动画对象。

最后,在需要进行转场动画的地方,我们可以调用 presentNextViewController() 方法来呈现下一个视图控制器,并设置其 transitioningDelegate 属性和 modalPresentationStyle 属性为相应的值。

通过自定义转场动画对象和转场代理对象,我们可以解决在 IOS 8.3 及更高版本中交互式转换忽略 UIView 动画完成块的问题。通过这种方式,我们可以在动画完成时执行自定义的代码,从而更灵活地管理视图控制器之间的过渡效果。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号