当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现多个弹框按顺序依次弹出效果

iOS实现多个弹框按顺序依次弹出效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

高达之究极技师,海贼王漫画720,同洲电子招聘

有时候会有这样的需求:app 运行完,加载 rootvc ,此时需要做一些操作,比如检查更新,之类的。此时可能会需要有2个甚至多个弹框依次弹出。

本篇将以系统的 uialertcontroller 作为示例,当然,如果是自定义的,也要看一下这篇文章,如何来处理多个弹窗。

首先,如果就按照如下的默认写法:

- (void)viewdidappear:(bool)animated {
 [super viewdidappear:animated];
 uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"弹框1" message:@"第一个弹框" preferredstyle:uialertcontrollerstylealert];
 [alert addaction:[uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:nil]];
 [self presentviewcontroller:alert animated:yes completion:nil];

 uialertcontroller *alert2 = [uialertcontroller alertcontrollerwithtitle:@"弹框2" message:@"第二个弹框" preferredstyle:uialertcontrollerstylealert];
 [alert2 addaction:[uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:nil]];
 [self presentviewcontroller:alert2 animated:yes completion:nil];
}

会有什么问题呢?注意控制台,肯定会输出

warning: attempt to present <uialertcontroller: 0x7ff4c3078c00>  on <sctestviewcontroller: 0x7ff4c2718c20> which is already presenting <uialertcontroller: 0x7ff4c283ae00>

所以说,第二个弹框应该是看不到的。

另一种情况,如果是自定义的 alert ,你把它 add 为 window 的子视图,这么做第二个弹框会盖在第一个上面。如果你用了毛玻璃背景,效果会更加明显。肯定不合适了。

所以,正确的解决办法就是类似加锁的过程,当点击了第一个弹框的某个按钮之后,再弹出第二个弹框,以此类瑞。

这里,我想到用信号量去解决,但是信号量会阻塞线程,不可以直接在主线程使用。所以我们需要在子线程控制信号量,在主线程创建和显示 alert,直接上代码。

- (void)viewdidappear:(bool)animated {
 [super viewdidappear:animated];
 //创建一个队列,串行并行都可以,主要为了操作信号量
 dispatch_queue_t queue = dispatch_queue_create("com.se7en.alert", dispatch_queue_serial);
 dispatch_async(queue, ^{
  //创建一个初始为0的信号量
  dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  //第一个弹框,ui的创建和显示,要在主线程
  dispatch_async(dispatch_get_main_queue(), ^{
   uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"弹框1" message:@"第一个弹框" preferredstyle:uialertcontrollerstylealert];
   [alert addaction:[uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:^(uialertaction * _nonnull action) {
    //点击alert上的按钮,我们发送一次信号。
    dispatch_semaphore_signal(sema);
   }]];
   [self presentviewcontroller:alert animated:yes completion:nil];
  });

  //等待信号触发,注意,这里是在我们创建的队列中等待
  dispatch_semaphore_wait(sema, dispatch_time_forever);
  //上面的等待到信号触发之后,再创建第二个alert
  dispatch_async(dispatch_get_main_queue(), ^{
   uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"弹框2" message:@"第二个弹框" preferredstyle:uialertcontrollerstylealert];
   [alert addaction:[uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:^(uialertaction * _nonnull action) {
    dispatch_semaphore_signal(sema);
   }]];
   [self presentviewcontroller:alert animated:yes completion:nil];
  });

  //同理,创建第三个alert
  dispatch_semaphore_wait(sema, dispatch_time_forever);
  dispatch_async(dispatch_get_main_queue(), ^{
   uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"弹框3" message:@"第三个弹框" preferredstyle:uialertcontrollerstylealert];
   [alert addaction:[uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:^(uialertaction * _nonnull action) {
    dispatch_semaphore_signal(sema);
   }]];
   [self presentviewcontroller:alert animated:yes completion:nil];
  });
 });
}

如此一来,就实现了我们的需求。

需要注意的是,这里为什么不用全局并发队列,主要是考虑到信号量会阻塞线程,优先级特别高,如果此时队列中还有任务,那么就会等待信号触发。当然也有人故意这么做。对于 “弹框弹出的时间,不要做其他任何事情” 这种需求是很合适的。当然我们千万不能去阻塞主线程!

我们在异步线程等待信号,在主线程发信号,如此就可以实现两个线程同步。其实信号量就是一种锁。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网