您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用Swift怎么實現一個最小化語音通話功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
import Foundation import UIKit enum SuspendType { case none case single case multi } class SuspendTool: NSObject { static let sharedInstance = SuspendTool() private var suspendWindows: [SuspendWindow] = [] // var semicircle: Semicircle? var origin: CGPoint = CGPoint.init(x: 10, y: 300) static func showSuspendWindow(rootViewController: UIViewController, coverImageName: String) { let tool = SuspendTool.sharedInstance let window = SuspendWindow.init(rootViewController: rootViewController, coverImageName: coverImageName, frame: CGRect.init(origin: tool.origin, size: CGSize.init(width: radious, height: radious))) window.show() tool.suspendWindows.append(window) } static func replaceSuspendWindow(rootViewController: UIViewController, coverImageName: String) { let tool = SuspendTool.sharedInstance tool.suspendWindows.removeAll() let window = SuspendWindow.init(rootViewController: rootViewController, coverImageName: coverImageName, frame: CGRect.init(origin: tool.origin, size: CGSize.init(width: radious, height: radious))) window.show() tool.suspendWindows.append(window) } static func remove(suspendWindow: SuspendWindow) { UIView.animate(withDuration: 0.25, animations: { suspendWindow.alpha = 0 }) { (complete) in if let index = SuspendTool.sharedInstance.suspendWindows.index(of: suspendWindow) { SuspendTool.sharedInstance.suspendWindows.remove(at: index) } } } static func setLatestOrigin(origin: CGPoint) { SuspendTool.sharedInstance.origin = origin } }
SuspendWindow
import UIKit let radious: CGFloat = 82 class SuspendWindow: UIWindow { fileprivate let coverImageName: String fileprivate let space: CGFloat = 15 var containsRootViewController: UIViewController? init(rootViewController: UIViewController ,coverImageName: String, frame: CGRect) { self.coverImageName = coverImageName super.init(frame: frame) // self.rootViewController = rootViewController self.containsRootViewController = rootViewController } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func show() { self.backgroundColor = UIColor.clear self.windowLevel = UIWindow.Level.alert - 1//UIWindowLevelAlert - 1 self.screen = UIScreen.main self.isHidden = false let bgView = UIView() bgView.isUserInteractionEnabled = true bgView.frame = self.bounds bgView.backgroundColor = UIColor.white bgView.layer.cornerRadius = radious / 2.0 bgView.layer.borderColor = UIColor.lightGray.cgColor bgView.layer.borderWidth = 5 bgView.layer.masksToBounds = true self.addSubview(bgView) bgView.addSubview(iconImageView) bgView.addSubview(timeLabel) let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(didPan(_:))) self.addGestureRecognizer(panGesture) let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:))) self.addGestureRecognizer(tapGesture) } @objc fileprivate func didTap(_ tapGesture: UITapGestureRecognizer) { SuspendTool.sharedInstance.origin = self.frame.origin self.containsRootViewController?.spread(from: self.self.frame.origin) SuspendTool.remove(suspendWindow: self) } @objc fileprivate func didPan(_ panGesture: UIPanGestureRecognizer) { let point = panGesture.translation(in: panGesture.view) var originX = self.frame.origin.x + point.x if originX < space { originX = space } else if originX > UIScreen.main.bounds.width - radious - space { originX = UIScreen.main.bounds.width - radious - space } var originY = self.frame.origin.y + point.y if originY < space { originY = space } else if originY > UIScreen.main.bounds.height - radious - space { originY = UIScreen.main.bounds.height - radious - space } self.frame = CGRect.init(x: originX, y: originY, width: self.bounds.width, height: self.bounds.height) if panGesture.state == UIGestureRecognizer.State.cancelled || panGesture.state == UIGestureRecognizer.State.ended || panGesture.state == UIGestureRecognizer.State.failed { self.adjustFrameAfterPan() } panGesture.setTranslation(CGPoint.zero, in: self) } fileprivate func adjustFrameAfterPan() { var originX: CGFloat = space if self.center.x < UIScreen.main.bounds.width / 2 { originX = space } else if self.center.x >= UIScreen.main.bounds.width / 2 { originX = UIScreen.main.bounds.width - radious - space } UIView.animate(withDuration: 0.25, animations: { self.frame = CGRect.init(x: originX, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height) }) { (complete) in SuspendTool.setLatestOrigin(origin: self.frame.origin) } } lazy var timeLabel: UILabel = { let timeLabel = UILabel() timeLabel.frame = CGRect(x: 0, y: 55.5, width: 42, height: 13) timeLabel.center.x = self.bounds.size.width / 2 timeLabel.textAlignment = .center timeLabel.text = "0:00" timeLabel.textColor = UIColor.text timeLabel.font = UIFont.mediumFont(ofSize: 13) return timeLabel }() lazy var iconImageView: UIImageView = { let iconImageView = UIImageView.init(image: UIImage.init(named: coverImageName)) iconImageView.isUserInteractionEnabled = true iconImageView.frame = CGRect(x: 0, y: 12, width: 38, height: 38) iconImageView.center.x = self.bounds.size.width / 2 return iconImageView }() }
UIViewController+FF
import Foundation import UIKit extension UIViewController { func suspend(coverImageName: String, type: SuspendType) { if type == .none { self.navigationController?.popViewController(animated: true) return } self.view.layer.masksToBounds = true UIView.animate(withDuration: 0.25, animations: { self.view.layer.cornerRadius = radious / 2.0 self.view.frame = CGRect.init(origin: SuspendTool.sharedInstance.origin, size: CGSize.init(width: radious, height: radious)) self.view.layoutIfNeeded() }) { (complete) in self.navigationController?.popViewController(animated: false) if type == .single { SuspendTool.replaceSuspendWindow(rootViewController: self, coverImageName: coverImageName) } else { SuspendTool.showSuspendWindow(rootViewController: self, coverImageName: coverImageName) } } } func spread(from point: CGPoint) { if let isContain = self.navigationController?.viewControllers.contains(self), isContain { return } self.view.frame = CGRect.init(origin: point, size: CGSize.init(width: radious, height: radious)) //UIViewController.currentViewController() UIViewController.currentViewController().navigationController?.pushViewController(self, animated: false) UIView.animate(withDuration: 0.25, animations: { self.view.layer.cornerRadius = 0 self.view.frame = UIScreen.main.bounds self.view.layoutIfNeeded() }) } static func currentViewController() -> UIViewController { var rootViewController: UIViewController? = nil for window in UIApplication.shared.windows { if (window.rootViewController != nil) { rootViewController = window.rootViewController break } } var viewController = rootViewController while (true) { if viewController?.presentedViewController != nil { viewController = viewController!.presentedViewController } else if viewController!.isKind(of: UINavigationController.self) { viewController = (viewController as! UINavigationController).visibleViewController } else if viewController!.isKind(of: UITabBarController.self) { viewController = (viewController as! UITabBarController).selectedViewController } else { break } } return viewController! } }
上述內容就是使用Swift怎么實現一個最小化語音通話功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。