一个弹窗提示
调用方式
ToastView.instance.showToast(str)
或自定义显示图片
ToastView.instance.showToast(str, imageName: "icon_sad")
详细代码
import UIKit
//弹窗工具
class ToastView : NSObject{
static var instance : ToastView = ToastView()
var windows = UIApplication.sharedApplication().windows
let rv = UIApplication.sharedApplication().keyWindow?.subviews.first as UIView!
//显示加载圈圈
func showLoadingView() {
clear()
let frame = CGRectMake(0, 0, 78, 78)
let loadingContainerView = UIView()
loadingContainerView.layer.cornerRadius = 12
loadingContainerView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
let indicatorWidthHeight :CGFloat = 36
let loadingIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
loadingIndicatorView.frame = CGRectMake(frame.width/2 - indicatorWidthHeight/2, frame.height/2 - indicatorWidthHeight/2, indicatorWidthHeight, indicatorWidthHeight)
loadingIndicatorView.startAnimating()
loadingContainerView.addSubview(loadingIndicatorView)
let window = UIWindow()
window.backgroundColor = UIColor.clearColor()
window.frame = frame
loadingContainerView.frame = frame
window.windowLevel = UIWindowLevelAlert
window.center = CGPoint(x: rv.center.x, y: rv.center.y)
window.hidden = false
window.addSubview(loadingContainerView)
windows.append(window)
}
//弹窗图片文字
func showToast(content:String , imageName:String="icon_cool", duration:CFTimeInterval=1.5) {
clear()
let frame = CGRectMake(0, 0, 90, 90)
let toastContainerView = UIView()
toastContainerView.layer.cornerRadius = 10
toastContainerView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.7)
let iconWidthHeight :CGFloat = 36
let toastIconView = UIImageView(image: UIImage(named: imageName)!)
toastIconView.frame = CGRectMake((frame.width - iconWidthHeight)/2, 15, iconWidthHeight, iconWidthHeight)
toastContainerView.addSubview(toastIconView)
let toastContentView = UILabel(frame: CGRectMake(0, iconWidthHeight + 5, frame.height, frame.height - iconWidthHeight))
toastContentView.font = UIFont.systemFontOfSize(13)
toastContentView.textColor = UIColor.whiteColor()
toastContentView.text = content
toastContentView.textAlignment = NSTextAlignment.Center
toastContainerView.addSubview(toastContentView)
let window = UIWindow()
window.backgroundColor = UIColor.clearColor()
window.frame = frame
toastContainerView.frame = frame
window.windowLevel = UIWindowLevelAlert
window.center = CGPoint(x: rv.center.x, y: rv.center.y * 16/10)
window.hidden = false
window.addSubview(toastContainerView)
windows.append(window)
toastContainerView.layer.addAnimation(AnimationUtil.getToastAnimation(duration), forKey: "animation")
performSelector(#selector(removeToast(_:)), withObject: window, afterDelay: duration)
}
//移除当前弹窗
func removeToast(sender: AnyObject) {
if let window = sender as? UIWindow {
if let index = windows.indexOf({ (item) -> Bool in
return item == window
}) {
// print("find the window and remove it at index \(index)")
windows.removeAtIndex(index)
}
}else{
// print("can not find the window")
}
}
//清除所有弹窗
func clear() {
NSObject.cancelPreviousPerformRequestsWithTarget(self)
windows.removeAll(keepCapacity: false)
}
}
其中,动画类
class AnimationUtil{
//弹窗动画
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: kCAMediaTimingFunctionLinear)
animation.duration = duration
animation.repeatCount = 0// HUGE
animation.removedOnCompletion = false
return animation
}
}