Swift:一個彈窗提示Toast

一個彈窗提示

toast.gif

調用方式

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
}
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,829評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • 庫表設計 庫名、表名、字段名使用小寫字母,”_”分割,不超過18 個字符,使用名詞且見名知意. 不使用temp、o...
    迷途的華生閱讀 1,030評論 0 10
  • 美好的第七周就結束了,這一周有很多快樂的事情,也有許多不愉快的事情,比如我的弟弟被人拿鐵砸了,我很生氣他為什么...
    黨士杰閱讀 160評論 0 0
  • 美好時光_4f81閱讀 307評論 0 0