前言
專案中做的一個Swift版本的翻牌影片,在自定義Window彈窗基礎上,使用transition影片實作,
效果圖

1.彈窗部分
彈窗考慮到解耦和使用便捷,采取自定義Window來實作,所有彈窗由一個可銷毀單例來統一管理,這樣可以很容易管理每個彈窗,也方便擴展其他型別的彈窗,當所有彈窗都全部銷毀后,單例自行銷毀
彈窗核心代碼
//MARK: - 彈窗管理者
@objcMembers class CLPopupManager: NSObject {
private static var manager: CLPopupManager?
private class var share: CLPopupManager {
get {
guard let shareManager = manager else {
manager = CLPopupManager()
return manager!
}
return shareManager
}
}
private var windowsDictionary = [String : CLPopupManagerWindow]()
private override init() {
super.init()
}
deinit {
// CLLog("===== \(self.classForCoder) deinit =====")
}
}
extension CLPopupManager {
/// 顯示自定義彈窗
private class func showController(_ controller: CLPopupManagerController) {
DispatchQueue.main.async {
if controller.configure.isDisplacement {
share.windowsDictionary.removeAll()
}
let window = CLPopupManagerWindow(frame: UIScreen.main.bounds)
window.isPassedDown = controller.configure.isPassedDown
window.windowLevel = UIWindow.Level.statusBar
window.isUserInteractionEnabled = true
window.rootViewController = controller
window.makeKeyAndVisible()
share.windowsDictionary[controller.configure.identifier] = window
}
}
/// 隱藏所有彈窗
class func dismissAll() {
DispatchQueue.main.async {
share.windowsDictionary.removeAll()
manager = nil
}
}
///隱藏指定彈窗
class func dismiss(_ identifier : String) {
DispatchQueue.main.async {
share.windowsDictionary.removeValue(forKey: identifier)
if share.windowsDictionary.isEmpty {
dismissAll()
}
}
}
}
2.影片部分
影片的核心采取系統transition影片實作,加上其他基礎影片組合而成
func transition(withDuration duration: TimeInterval, completion: (() -> Void)?) {
guard let top = topView, let bottom = bottomView else {
return
}
if isTurning {
return
}
isTurning = true
if isReversed {
UIView.transition(from: bottom, to: top, duration: duration, options: .transitionFlipFromLeft) { _ in
completion?()
self.isTurning = false
self.isReversed = false
}
} else {
UIView.transition(from: top, to: bottom, duration: duration, options: .transitionFlipFromRight) { _ in
completion?()
self.isTurning = false
self.isReversed = true
}
}
}
3.其他彈窗



作為一個開發者,有一個學習的氛圍跟一個交流圈子特別重要,這是一個我的iOS交流群:196800191,加群密碼:112233,不管你是小白還是大牛歡迎入駐 ,分享BAT,阿里面試題、面試經驗,討論技術, 大家一起交流學習成長!
總結
核心代碼已經貼出,完整代碼請查看----->>>CLDemo,如果對你有所幫助,歡迎Star,
作者:季末微夏
鏈接:https://www.jianshu.com/p/e53e99a686ec
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/232677.html
標籤:其他
