Skip to content

Commit 399ab89

Browse files
author
zhaoyongqiang
committed
add alertManager
1 parent 4e734ac commit 399ab89

2 files changed

Lines changed: 181 additions & 4 deletions

File tree

iOS/APIExample-Audio/APIExample-Audio.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
E70EE9E828F6A6FF00EE9E7F /* JoinChannelAudioToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = E70EE9E428F6A6FF00EE9E7F /* JoinChannelAudioToken.swift */; };
6363
E70EE9E928F6A6FF00EE9E7F /* JoinChannelAudioToken.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E70EE9E528F6A6FF00EE9E7F /* JoinChannelAudioToken.storyboard */; };
6464
E72055EC28F94C400030E6D1 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = E72055EB28F94C400030E6D1 /* Util.swift */; };
65-
E721601128D415C4006431BD /* AlertManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E721601028D415C4006431BD /* AlertManager.swift */; };
6665
E74877B728A23B8B00CA2F58 /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74877B628A23B8B00CA2F58 /* NetworkManager.swift */; };
6766
E74877BA28A23C1400CA2F58 /* JSONObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74877B928A23C1400CA2F58 /* JSONObject.swift */; };
6867
E74877CA28A2611C00CA2F58 /* ToastView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74877C928A2611C00CA2F58 /* ToastView.swift */; };
@@ -71,6 +70,7 @@
7170
E74878A628ADE21200CA2F58 /* RhythmPlayer.strings in Resources */ = {isa = PBXBuildFile; fileRef = E74878A128ADE21200CA2F58 /* RhythmPlayer.strings */; };
7271
E74878A728ADE21200CA2F58 /* RhythmPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74878A328ADE21200CA2F58 /* RhythmPlayer.swift */; };
7372
E74878A828ADE21200CA2F58 /* RhythmPlayer.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E74878A428ADE21200CA2F58 /* RhythmPlayer.storyboard */; };
73+
E7A49CBC29023BD300F06DD4 /* AlertManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7A49CBB29023BD300F06DD4 /* AlertManager.swift */; };
7474
/* End PBXBuildFile section */
7575

7676
/* Begin PBXCopyFilesBuildPhase section */
@@ -188,7 +188,6 @@
188188
E70EE9E428F6A6FF00EE9E7F /* JoinChannelAudioToken.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinChannelAudioToken.swift; sourceTree = "<group>"; };
189189
E70EE9E628F6A6FF00EE9E7F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/JoinChannelAudioToken.storyboard; sourceTree = "<group>"; };
190190
E72055EB28F94C400030E6D1 /* Util.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = "<group>"; };
191-
E721601028D415C4006431BD /* AlertManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertManager.swift; path = ../../../APIExample/APIExample/Common/AlertManager.swift; sourceTree = "<group>"; };
192191
E74877B628A23B8B00CA2F58 /* NetworkManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
193192
E74877B928A23C1400CA2F58 /* JSONObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONObject.swift; sourceTree = "<group>"; };
194193
E74877C928A2611C00CA2F58 /* ToastView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = "<group>"; };
@@ -197,6 +196,7 @@
197196
E74878A228ADE21200CA2F58 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/RhythmPlayer.strings"; sourceTree = "<group>"; };
198197
E74878A328ADE21200CA2F58 /* RhythmPlayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RhythmPlayer.swift; sourceTree = "<group>"; };
199198
E74878A528ADE21200CA2F58 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/RhythmPlayer.storyboard; sourceTree = "<group>"; };
199+
E7A49CBB29023BD300F06DD4 /* AlertManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertManager.swift; sourceTree = "<group>"; };
200200
EAD308B056B63304DA681699 /* Pods-Agora-ScreenShare-Extension(Socket).release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Agora-ScreenShare-Extension(Socket).release.xcconfig"; path = "Target Support Files/Pods-Agora-ScreenShare-Extension(Socket)/Pods-Agora-ScreenShare-Extension(Socket).release.xcconfig"; sourceTree = "<group>"; };
201201
EB8CDD3F04870C6A31287732 /* Pods_audioFilter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_audioFilter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
202202
FAAC2AEE355D103B9E8527B5 /* Pods-Agora-ScreenShare-Extension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Agora-ScreenShare-Extension.debug.xcconfig"; path = "Target Support Files/Pods-Agora-ScreenShare-Extension/Pods-Agora-ScreenShare-Extension.debug.xcconfig"; sourceTree = "<group>"; };
@@ -358,11 +358,11 @@
358358
0318854D24CD5F9000C699EB /* Settings */,
359359
03D13C0024488F1E00B599B3 /* KeyCenter.swift */,
360360
A7847F932458089E00469187 /* AgoraExtension.swift */,
361+
E7A49CBB29023BD300F06DD4 /* AlertManager.swift */,
361362
E748789C28ADE03400CA2F58 /* VideoView.swift */,
362363
E748789D28ADE03400CA2F58 /* VideoView.xib */,
363364
A7847F912458062900469187 /* StatisticsInfo.swift */,
364365
03BCEC4F244938C500ED7177 /* BaseViewController.swift */,
365-
E721601028D415C4006431BD /* AlertManager.swift */,
366366
03BCEC752449EB4F00ED7177 /* LogViewController.swift */,
367367
A7BD765F247CC6920062A6B3 /* UITypeAlias.swift */,
368368
03F8733124C8696600EDB1A3 /* EntryViewController.swift */,
@@ -692,8 +692,8 @@
692692
03DF1D9024CFC29700DF7151 /* AudioWriteToFile.m in Sources */,
693693
E72055EC28F94C400030E6D1 /* Util.swift in Sources */,
694694
0339BE6D251DEAFC007D4FDD /* PrecallTest.swift in Sources */,
695-
E721601128D415C4006431BD /* AlertManager.swift in Sources */,
696695
034C625E2524A06800296ECF /* VoiceChanger.swift in Sources */,
696+
E7A49CBC29023BD300F06DD4 /* AlertManager.swift in Sources */,
697697
03D13BD02448758900B599B3 /* AppDelegate.swift in Sources */,
698698
A7847F922458062900469187 /* StatisticsInfo.swift in Sources */,
699699
03BCEC762449EB5000ED7177 /* LogViewController.swift in Sources */,
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//
2+
// AlertManager.swift
3+
// Scene-Examples
4+
//
5+
// Created by zhaoyongqiang on 2021/11/10.
6+
//
7+
8+
import UIKit
9+
import AVFoundation
10+
11+
public let cl_screenWidht = UIScreen.main.bounds.width
12+
public let cl_screenHeight = UIScreen.main.bounds.height
13+
class AlertManager: NSObject {
14+
private struct AlertViewCache {
15+
var view: UIView?
16+
var index: Int = 0
17+
}
18+
enum AlertPosition {
19+
case center
20+
case bottom
21+
}
22+
23+
private static var vc: UIViewController?
24+
private static var containerView: UIView?
25+
private static var currentPosition: AlertPosition = .center
26+
private static var viewCache: [AlertViewCache] = []
27+
private static var bottomAnchor: NSLayoutConstraint?
28+
29+
public static func show(view: UIView,
30+
alertPostion: AlertPosition = .center,
31+
didCoverDismiss: Bool = true) {
32+
let index = viewCache.isEmpty ? 0 : viewCache.count
33+
viewCache.append(AlertViewCache(view: view, index: index))
34+
currentPosition = alertPostion
35+
if vc == nil {
36+
containerView = UIButton(frame: CGRect(x: 0, y: 0, width: cl_screenWidht, height: cl_screenHeight))
37+
containerView?.backgroundColor = UIColor(red: 0.0/255, green: 0.0/255, blue: 0.0/255, alpha: 0.0)
38+
}
39+
if didCoverDismiss {
40+
(containerView as? UIButton)?.addTarget(self, action: #selector(tapView), for: .touchUpInside)
41+
}
42+
guard let containerView = containerView else { return }
43+
containerView.addSubview(view)
44+
view.translatesAutoresizingMaskIntoConstraints = false
45+
view.alpha = 0
46+
if alertPostion == .center {
47+
view.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
48+
view.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
49+
}else{
50+
bottomAnchor = view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
51+
view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
52+
view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
53+
}
54+
if vc == nil {
55+
vc = UIViewController()
56+
vc?.view.backgroundColor = UIColor.clear
57+
vc?.view.addSubview(containerView)
58+
vc?.modalPresentationStyle = .custom
59+
UIViewController.cl_topViewController()?.present(vc!, animated: false) {
60+
showAlertPostion(alertPostion: alertPostion, view: view)
61+
}
62+
} else {
63+
showAlertPostion(alertPostion: alertPostion, view: view)
64+
}
65+
//注册键盘出现通知
66+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)
67+
68+
//注册键盘隐藏通知
69+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
70+
}
71+
72+
private static func showAlertPostion(alertPostion: AlertPosition, view: UIView) {
73+
containerView?.layoutIfNeeded()
74+
if alertPostion == .center {
75+
showCenterView(view: view)
76+
}else{
77+
bottomAnchor?.constant = view.frame.height
78+
bottomAnchor?.isActive = true
79+
containerView?.layoutIfNeeded()
80+
showBottomView(view: view)
81+
}
82+
}
83+
84+
private static func showCenterView(view: UIView){
85+
if !viewCache.isEmpty {
86+
viewCache.forEach({ $0.view?.alpha = 0 })
87+
}
88+
UIView.animate(withDuration: 0.25, animations: {
89+
containerView?.backgroundColor = UIColor(red: 0.0/255,
90+
green: 0.0/255,
91+
blue: 0.0/255,
92+
alpha: 0.5)
93+
view.alpha = 1.0
94+
})
95+
}
96+
97+
private static func showBottomView(view: UIView){
98+
if !viewCache.isEmpty {
99+
viewCache.forEach({ $0.view?.alpha = 0 })
100+
}
101+
view.alpha = 1.0
102+
bottomAnchor?.constant = 0
103+
bottomAnchor?.isActive = true
104+
UIView.animate(withDuration: 0.25, animations: {
105+
containerView?.backgroundColor = UIColor(red: 0.0/255,
106+
green: 0.0/255,
107+
blue: 0.0/255,
108+
alpha: 0.5)
109+
containerView?.superview?.layoutIfNeeded()
110+
})
111+
}
112+
113+
static func updateViewHeight() {
114+
UIView.animate(withDuration: 0.25, animations: {
115+
containerView?.layoutIfNeeded()
116+
})
117+
}
118+
119+
static func hiddenView(all: Bool = true, completion: (() -> Void)? = nil){
120+
if currentPosition == .bottom {
121+
guard let lastView = viewCache.last?.view else { return }
122+
bottomAnchor?.constant = lastView.frame.height
123+
bottomAnchor?.isActive = true
124+
}
125+
UIView.animate(withDuration: 0.25, animations: {
126+
if all || viewCache.isEmpty {
127+
containerView?.backgroundColor = UIColor(red: 255.0/255,
128+
green: 255.0/255,
129+
blue: 255.0/255,
130+
alpha: 0.0)
131+
containerView?.layoutIfNeeded()
132+
}
133+
if currentPosition == .center {
134+
viewCache.last?.view?.alpha = 0
135+
}
136+
}, completion: { (_) in
137+
if all || viewCache.isEmpty {
138+
vc?.dismiss(animated: false, completion: completion)
139+
vc = nil
140+
} else {
141+
viewCache.removeLast()
142+
viewCache.last?.view?.alpha = 1
143+
}
144+
})
145+
}
146+
147+
@objc
148+
private static func tapView(){
149+
DispatchQueue.main.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: UInt64(0.1))) {
150+
self.hiddenView()
151+
}
152+
}
153+
154+
private static var originFrame:CGRect = .zero
155+
// 键盘显示
156+
@objc private static func keyboardWillShow(notification: Notification) {
157+
let keyboardHeight = (notification.userInfo?["UIKeyboardBoundsUserInfoKey"] as? CGRect)?.height
158+
let y = cl_screenHeight - (keyboardHeight ?? 304) - containerView!.frame.height
159+
if originFrame.origin.y != y {
160+
originFrame = containerView!.frame
161+
}
162+
UIView.animate(withDuration: 0.25) {
163+
containerView?.frame.origin.y = y
164+
}
165+
}
166+
// 键盘隐藏
167+
@objc private static func keyboardWillHide(notification: Notification) {
168+
UIView.animate(withDuration: 0.25) {
169+
containerView?.frame = originFrame
170+
} completion: { _ in
171+
if currentPosition == .bottom {
172+
hiddenView()
173+
}
174+
}
175+
}
176+
}
177+

0 commit comments

Comments
 (0)