IOS 实现 Toast 的原理
2019-06-17 本文已影响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)
}
})
})
}
}