swift版AlertView顺序弹出

项目中有个需求,从N个接口中返回N个弹出窗,要求按顺序弹出,也就是一个关闭,另外一个才能弹出,于是写了一个管理类,顺手记录下来。
代码:

import UIKit

class AlertViewManager {
    static let share = AlertViewManager()
    let semaphore = DispatchSemaphore(value: 1)
    let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent)
    func showWithExecuteClosure(_ showClosure: Closure?) {
        concurrentQueue.async {
            self.semaphore.wait()
            DispatchQueue.main.async {
                if showClosure != nil {
                    showClosure?()
                }
            }
        }
    }
    func dismissWithExecuteClosure(_ dissMissClosure: Closure? = nil) {
        concurrentQueue.async {
            self.semaphore.signal()
            DispatchQueue.main.async {
                if dissMissClosure != nil {
                    dissMissClosure?()
                }
            }
        }
    }
}

使用方式:

for _ in 0...3 {
        // 先占用资源,
        AlertViewManager.share.showWithExecuteClosure {
            // 显示你的Alert(不一定是alert, 甚至可以是一些逻辑处理)
            AdvertisingAlertVC.showWith(advImageUrl: advModel.pic) {[weak self](isPush) in
                // 在你的alert的消失闭包回调里在释放资源(必须和占用资源成对出现)
                AlertViewManager.share.dismissWithExecuteClosure()
                
                }
            }
        }
    }

注:Closure 是项目里定义的一个无参无返回值的闭包

你可能感兴趣的:(swift版AlertView顺序弹出)