自定义modal弹出样式(swift)
来源:互联网 发布:杭州网络诈骗最新新闻 编辑:程序博客网 时间:2024/06/04 18:56
0.假设vc是需要弹出的控制器(因为弹窗中有需要处理的事务,所以使用一个控制器管理)
1.弹出菜单(使用modal将对应的控制器弹出)
presentViewController(vc, animated: true, completion: nil)
2.如果想要自己决定弹出方式,需要成为其代理
vc.transitioningDelegate = selfvc.modalPresentationStyle = .Custom
3.实现transitioningDelegate的代理方法
//该代理方法用于返回负责转场的控制器对象func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting) }
//该代理方法用于告诉系统谁来负责控制器如何弹出func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { //当弹窗要弹出的时候就会调用该方法 return self }
//该代理方法用于告诉系统谁来负责控制器如何消失func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { //当弹窗要消失的时候就会调用该方法 return self }
4.自定义显示和消失时弹窗展现方式
// 该方法用于负责控制器如何弹出和如何消失 // 只要是自定义转场, 控制器弹出和消失都会调用该方法 // 需要在该方法中告诉系统控制器如何弹出和如何消失 // 注意点: 但凡告诉系统我们需要自己来控制控制器的弹出和消失 // 也就是实现了UIViewControllerAnimatedTransitioning的方法之后, 那么系统就不会再控制我们控制器的动画了, 所有的操作都需要我们自己完成 // 系统调用该方法时会传递一个transitionContext参数, 该参数中包含了我们所有需要的值 func animateTransition(transitionContext: UIViewControllerContextTransitioning) { //在这里自定义出现和消失时的动画 }
5.自定义蒙版
注意点:
所有被弹出的内容, 都是放在容器视图上的说明:因为当弹窗出现的时候,背后的控制器是不能处理事件的,所以将继承自 UIPresentationController的控制器(VC2)的containerView中添加一个UIBottom作为蒙版;
因为UIBottom可以被监听,在VC2中监听蒙版,一旦点击,就是要消失弹窗;
继承自UIPresentationController的控制器负责管理自定义转场动画
在VC2中的 containerViewWillLayoutSubviews方法中设置弹窗的尺寸和位置,在 presentationTransitionWillBegin方法中添加蒙版,因为弹出的内容,都是放在容器视图中的,所以将蒙版添加到containerView中;在该方法中,containerView已经被创建;
6.设置显示时动画
因为显示和消失动画都是在第4步中的animateTransition方法中设置的,但是怎么区分是出现还是消失弹窗呢?
在第3步中,
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { //当弹窗要弹出的时候就会调用该方法 isPresent = true return self}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { //当弹窗要消失的时候就会调用该方法 isPresent = false return self }
所以设置一个属性isPresent,当是弹出时,在animationControllerForPresentedController方法中设置为true;
如果是小数,在animationControllerForDismissedController中设置为false;这样在第4步中的animateTransition方法中设置动画则根据属性的值进行设置;
当isPresent = true 时:(抽取一个方法)
/// 弹出动画 private func animatePresentedController(transitionContext: UIViewControllerContextTransitioning) { // 0.获取动画时长 let duration = transitionDuration(transitionContext) // 1.拿到被弹出的控制器的View guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else { return } // 2.将被弹出的控制器View添加到容器视图上 transitionContext.containerView()?.addSubview(toView) // 3.执行动画 // 3.1先将菜单的View压扁 toView.transform = CGAffineTransformMakeScale(1.0, 0.0) toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0) // 3.2再清空菜单压扁的形变 UIView.animateWithDuration(duration, animations: { () -> Void in toView.transform = CGAffineTransformIdentity }) { (_) -> Void in // 注意点: 但凡是自定义转场, 一定要在自定义动画完成之后告诉系统动画已经完成了, 否则会出现一些未知异常 transitionContext.completeTransition(true) } }
7.设置消失动画
//动画消失 private func animateDismissedController(transitionContext: UIViewControllerContextTransitioning) { // 0.获取动画时长 let duration = transitionDuration(transitionContext) // 1.拿到菜单 let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) // 2.执行动画 UIView.animateWithDuration(duration, animations: { () -> Void in // 注意; 没有动画的原因是MakeScale接收的是CGFloat, 而CGFloat得值是不准确的 fromView?.transform = CGAffineTransformMakeScale(1.0, 0.0001) }) { (_) -> Void in transitionContext.completeTransition(true) } }
0 0
- 自定义modal弹出样式(swift)
- ionic3/2 使用modal自定义弹出框
- 用swift写一个简单的自定义modal转场动画
- React Native学习之Modal控件自定义弹出View
- React Native学习之Modal控件自定义弹出View
- bootstrap modal弹出框
- angularjs 弹出框 $modal
- bootstrap modal 弹出失效
- Modal弹出框
- angularjs 弹出框 $modal
- 弹出模态框modal示例
- modal呈现样式
- modal 控制器的样式
- Swift 百度地图自定义弹出视图
- android自定义弹出框样式实现
- Android Dialog 弹出框 自定义 样式
- android自定义弹出框样式实现
- android自定义弹出框样式实现
- 转载:Android动画学习笔记-Android Animation
- 一句移除所有想移除的子视图
- 使用MJRefreshFooter引起的tableview第一行无法选中的问题
- 内置浏览器返回键重定向问题解析
- C#三十六 三层架构的实现
- 自定义modal弹出样式(swift)
- POJ 1013 (简单模拟)
- 百度DSP帐户搭建超全流程介绍
- C++ IPv4与IPv6的兼容编码
- Hadoop 之 Shuffle and Sort
- android唯一机器码生成方案
- Node.js、Express、Socket.io 入门
- --Notepad++快捷键 单行、多行、区块注释
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)