美文网首页
IOS 实现 Toast 的原理

IOS 实现 Toast 的原理

作者: 贼噶人 | 来源:发表于2019-06-17 22:19 被阅读0次
        @objc func touchDown(){
            if let button = button {
                button.backgroundColor = UIColor.cyan
            }
            let toast = UILabel.init(frame: CGRect.init(x: UIScreen.main.bounds.width / 4, y: UIScreen.main.bounds.height/2, width: UIScreen.main.bounds.width/2, height:50))
            if let keyWindow = UIApplication.shared.keyWindow {
                toast.backgroundColor = UIColor.white
                keyWindow.addSubview(toast)
                toast.textColor = UIColor.black
                toast.text = "This is toast!"
                toast.baselineAdjustment = UIBaselineAdjustment.alignCenters
                toast.textAlignment = NSTextAlignment.center
                toast.alpha = 0.2
                keyWindow.bringSubviewToFront(toast)
                UIView.animate(withDuration: 0.3, animations: {
                    toast.alpha = 1
                }, completion: {
                    f in
                    UIView.animate(withDuration: 0.3, animations: {
                        toast.alpha = 0
                    }, completion: {
                        f in
                        if f {
                            keyWindow.willRemoveSubview(toast)
                        }
                        
                    })
                })
                
            }
        }
    
    

    相关文章

      网友评论

          本文标题:IOS 实现 Toast 的原理

          本文链接:https://www.haomeiwen.com/subject/xoltqctx.html