ios - 通过导航控制器使用自定义动画转换到 SecondViewController 后,SecondViewController 中的非活动视图
问题描述
我有 2 个视图控制器(ViewController 和 SecondViewController)和 1 个导航控制器:
我的项目使用从 ViewController 到 SecondViewController 的自定义过渡动画:
import UIKit
class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.8
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
var transform = CATransform3DIdentity
let container = transitionContext.containerView
guard let fromView = transitionContext.view(forKey: .from) else { return }
guard let toView = transitionContext.view(forKey: .to) else { return }
transform.m34 = -0.0019
container.layer.sublayerTransform = transform
container.insertSubview(toView, belowSubview: fromView)
fromView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.5)
fromView.layer.position = CGPoint(x: 0, y: UIScreen.main.bounds.midY)
UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
}
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
在 ViewController(其中门图像)中,我使用延迟来启动过渡动画并转到 SecondViewController(其中绿色背景)。
class ViewController: UIViewController {
let transitionManager = TransitionManager()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
self.performSegue(withIdentifier: "segue", sender: self)
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let navigationViewController = segue.destination as? UINavigationController else { return }
navigationViewController.transitioningDelegate = transitionManager
}
}
SecondViewController 具有按钮按下功能:
class SecondViewController: UIViewController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
print(view.isUserInteractionEnabled)
}
@IBAction func tapBtn(_ sender: UIButton) {
print("btn pressed") //it is not worked. Why?
}
}
动画正常工作,但 SecondViewController(绿色背景)不响应按钮按下。SecondViewController 中的代码
print(view.isUserInteractionEnabled)
返回真。我的代码中的问题在哪里?我想按下按钮来工作。
解决方案
解决方案。在 TransitionManager 中删除它:
UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
}
并添加:
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, options: .curveEaseInOut, animations: {
fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
}) { (finished) in
transitionContext.completeTransition(true)
}
推荐阅读
- c++builder - 如何解决 RAD Studio 10.3.3 中的“找不到入口点”错误?
- python - 算法将 365 天转换为月份和月份中的某天(波斯历)
- r - 在 R 中的 mapdeck/mapbox 中保持相同视图的同时清除数据
- authentication - 是否可以增加 google-authenticator PAM(Pluggable authentication Module)生成的代码的默认时间段?
- javascript - 你能在没有 PHP 的情况下制作一个电子邮件发送系统吗?
- bootstrap-4 - react-bootstrap 如何在单击切换时将折叠中的项目向左对齐
- javascript - 这是我的 javascript 代码,用于检查正则表达式,如用户名验证、密码,但没有得到任何输出
- c - 为什么环境变量(PATH)在 bash 中工作?
- c# - C# System.InvalidOperationException 错误
- excel - 为什么最后一个文件是 vba 中的第一个文件?