ios AlertController之用viewcontro
最近又做了一波弹窗。。之前大部分都是直接搬运一波,偶尔自己写一波。。。为了能让自己以后还有口饭吃。。。再决定写一波

写弹窗以前在[UIApplication sharedApplication].keyWindow 或者[[[UIApplication sharedApplication] delegate] window]上直接添加一个view。。简单粗暴,方便快捷。。
这样做也没啥不好。
但是,今天的我,与众不同。。仔细研究了一下viewcontroller的模态显示。。viewcontroller的专场一般有以下几种:
1.在 UINavigationController 中 push 和 pop;
2.在 UITabBarController 中切换 Tab;
3.Modal 转场:presentation 和 dismissal,俗称视图控制器的模态显示和消失。
今天我就用到了Modal专场
在我们present一个VC的时候把控制器的modalPresentationStyle 设置为UIModalPresentationOverFullScreen。为啥要这样设置?因为这样可以使下一个控制器变为半透明。接下来,在present的时候 直接关闭动画效果。这样就模仿了系统的弹窗显示样式。
如下图:

OK。。。阴影蒙板效果搞出来之后,我们就在上面可以布局了。。。想怎么搞就怎么搞。。然后我就搞出了上面的样式。。。
之后,我们在弹窗控制器上添加单击手势,点击关闭弹窗效果。
布局完成之后,添加一下动画效果。
[UIView animateWithDuration:0.35 animations:^{
__strongtypeof(weakSelf) strongSelf = weakSelf;
[strongSelf.pushListViewsetFrame:CGRectMake(0,UIScreenHeight-modulsH -140-15-20,UIScreenWidth, modulsH +140-15-20)];
}];
在上面的关闭按钮添加
[UIView animateWithDuration:0.35 animations:^{
__strongtypeof(weakSelf) strongSelf = weakSelf;
[strongSelf.pushListViewsetFrame:CGRectMake(0,UIScreenHeight,UIScreenWidth,0)];
strongSelf.view.alpha=0;
}completion:^(BOOLfinished) {
if(finished) {
[weakSelfdismissViewControllerAnimated:NO completion:nil];
}
}];
哦。。记得,动画效果不要放在viewDidLoad中,放在viewWillAppear,不然会和控制器的转场动画冲突,导致不显示动画。。。
好了,接下来附上GitHub连接,有需要的小伙伴可以直接搬运去看看,有问题请大佬多多指点。
https://github.com/huangdougod/WYPopVC_Demo/tree/master
GitHub - huangdougod/WYPopVC_Demo: 一个基于模态viewcontroller的视图弹窗
恩,进步虽然少,但是也得慢慢往前爬么。。。。

2018.12.3:将window层添加弹窗的模式放到demo里了。