ios - 呈现 UIViewController 时如何为视图设置动画?
问题描述
我有一个习惯alert
,它UIView
在UIViewController
. 因此,当我想呈现alert
Ipresent
时UIViewController
,就会出现alert
. 我遇到的问题是我想alert
在它显示的时候做一个简单的动画。但由于它以 a 的形式呈现,UIViewController
而不仅仅是以UIView
覆盖图的形式显示,因此我对如何实现这一点感到有些困惑。以下是相关代码:
static func standardMessageAlert(title: String, msg: String, action: ((_ text: String?) -> Void)? = nil) -> CustomAlertViewController? {
let alert = CustomAlertViewController.create(title: title, message: msg)
let okAction = CustomAlertAction(title: "OK", type: .normal, handler: action)
alert?.addAction(okAction)
return alert
}
的代码CustomAlertViewController
非常大,但 create 方法如下所示:
static func create(title: String?, message: String?, addon: Addon? = nil, alertType: AlertType? = nil) -> CustomAlertViewController? {
let storyboard = UIStoryboard(name: "Overlay", bundle: nil)
guard let viewController = storyboard.instantiateViewController(withIdentifier: "CustomAlertViewController") as? CustomAlertViewController else {
return nil
}
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
viewController.alertType = alertType
viewController.addon = addon
viewController.alertTitle = title
viewController.alertMessage = message
return viewController
}
...
alert
如下所示:
func present(animated: Bool, onView: UIViewController? = UIApplication.rootViewController(), completion: (() -> Void)? = nil) {
onView?.present(self, animated: animated, completion: nil)
}
动画将是这样的基本内容:
func animateView() {
alertView.alpha = 0
UIView.animate(withDuration: 1, animations: { [weak self] () -> Void in
guard let self = self else { return }
self.alertView.alpha = 1.0
})
}
alertView
是里面outlet
的alert
UIView
那个UIViewController
。我想这是我应该制作动画的,但我该把代码放在哪里呢?出现时如何实现它UIViewController
?我也试过用onView.view
它来制作动画。我看到的问题是,如果我将动画代码放在presenting
它之前为时过早,而如果它在呈现之后则为时已晚。
解决方案
如果你想保持这个逻辑,那么我可以建议你设置初始视图状态viewDidLoad
并在之后运行动画viewWillAppear
。但我建议使用自定义过渡
推荐阅读
- html - 同时设置标题、字体大小和颜色
- python-3.x - 我可以使用 pipenv 创建两个虚拟环境,每个环境都有不同的 python 版本(即 3.7 和 3.6)吗?
- r - 如何总计几列到某个点?
- node.js - node js app.use 抛出一个中间件函数
- java - 在两个线程中交替打印字符串
- python - SyntaxError:yt 模块的语法无效
- r - 将线性回归与对数线性回归与 R 进行比较
- c# - C#/PHP 如何构建不能被滥用的 API
- python - Mathematica 的 NDSolve 和 SciPy 的 solve_ivp 返回不同的结果
- html - 网格中的多个按钮