windows: 可以有多个windows,每个alertView都是加在单独的一个window上的
keyWindow: 当前活跃的window
下面两句代码相同
let rv = UIApplication.shared.windows.last
let rv = UIApplication.shared.keyWindow?.subviews.first as UIView!
封装加载圈
window -> view -> UIActivityIndicatorView
//清除所有弹窗
func clear()
{
NSObject.cancelPreviousPerformRequests(withTarget: self)
windows.removeAll(keepingCapacity: false)
}
//显示加载圈圈
func showLoadingView()
{
clear()
//添加window
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 78, height: 78))
window.center = CGPoint(x: (windowLastView?.center.x)!, y: (windowLastView?.center.y)!)
window.backgroundColor = UIColor.clear
window.windowLevel = UIWindowLevelAlert
window.isHidden = false
windows.append(window)
//添加view
let loadingContainerView = UIView(frame: CGRect(x: 0, y: 0, width: 78, height: 78))
loadingContainerView.layer.cornerRadius = 12
loadingContainerView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
window.addSubview(loadingContainerView)
//添加UIActivityIndicatorView
let loadingIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
loadingIndicatorView.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
loadingIndicatorView.center = CGPoint(x: loadingContainerView.center.x, y: loadingContainerView.center.y)
loadingIndicatorView.startAnimating()
loadingContainerView.addSubview(loadingIndicatorView)
//3秒后消失
perform(#selector(removeToast(sender: )), with: window, afterDelay: 3)
}
封装文字提示框
window -> label
//弹窗文字
func showToast(content:String , duration:CFTimeInterval)
{
clear()
//添加windows
let window = UIWindow()
window.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
window.backgroundColor = UIColor.clear
window.windowLevel = UIWindowLevelAlert
window.isHidden = false
windows.append(window)
//添加label,显示提示语
let toastContentView = UILabel(frame:CGRect(x: 0, y: 0, width: 150, height: 20))
toastContentView.center = CGPoint(x: window.center.x, y: window.center.y)
toastContentView.textColor = UIColor.white
toastContentView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.7)
toastContentView.font = UIFont.systemFont(ofSize: 13)
toastContentView.numberOfLines = 0
toastContentView.text = content
toastContentView.textAlignment = NSTextAlignment.center
toastContentView.clipsToBounds = true
toastContentView.layer.cornerRadius = 7
toastContentView.sizeToFit()
toastContentView.width += 20
toastContentView.height += 20
window.addSubview(toastContentView)
//动画效果及延时后调移除窗口方法
toastContentView.layer.add(AnimationUtil.getToastAnimation(duration: duration), forKey: "animation")
perform(#selector(removeToast(sender: )), with: window, afterDelay: duration)
}
//移除当前弹窗
func removeToast(sender: AnyObject)
{
if let window = sender as? UIWindow
{
if let index = windows.index(where: { (item) -> Bool in
return item == window
})
{
print("find the window and remove it at index \(index)")
windows.remove(at: index)
}
}
else
{
print("can not find the window")
}
}
动画封装
import UIKit
class AnimationUtil: NSObject
{
//弹窗动画
static func getToastAnimation(duration:CFTimeInterval = 1.5) -> CAAnimation
{
// 大小变化动画
let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
scaleAnimation.keyTimes = [0, 0.1, 0.9, 1]
scaleAnimation.values = [0.5, 1, 1,0.5]
scaleAnimation.duration = duration
// 透明度变化动画
let opacityAnimaton = CAKeyframeAnimation(keyPath: "opacity")
opacityAnimaton.keyTimes = [0, 0.8, 1]
opacityAnimaton.values = [0.5, 1, 0]
opacityAnimaton.duration = duration
// 组动画
let animation = CAAnimationGroup()
animation.animations = [scaleAnimation, opacityAnimaton]
//动画的过渡效果1. kCAMediaTimingFunctionLinear//线性 2. kCAMediaTimingFunctionEaseIn//淡入 3. kCAMediaTimingFunctionEaseOut//淡出4. kCAMediaTimingFunctionEaseInEaseOut//淡入淡出 5. kCAMediaTimingFunctionDefault//默认
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
animation.duration = duration
animation.repeatCount = 0// HUGE
animation.isRemovedOnCompletion = false
return animation
}
}