func infoView(message:String, color:UIColor){
let errorViewHeight = view.frame.height / 14
let errorViewY = 0 - errorViewHeight
let errorView = UIView(frame: CGRect(x: 0, y: errorViewY , width: view.frame.width , height: errorViewHeight))
//let redColor = UIColor.init(red: 255/255, green: 50/255, blue: 75/255, alpha: 1)
errorView.backgroundColor = color
view.addSubview(errorView)
// 新增一個 Label
let errorLabel = UILabel()
errorLabel.frame.size.width = errorView.frame.width
errorLabel.frame.size.height = errorView.frame.height
errorLabel.text = message
errorLabel.textColor = UIColor.white
errorLabel.textAlignment = NSTextAlignment.center
errorLabel.font = UIFont(name: "HelveticaNeue", size: 18)
errorView.addSubview(errorLabel)
// 使用動畫的方式
// withDuration -> 動畫時間
// animation -> 結束的狀態
// completion -> 結束時要做什麼
// 一開始的動畫 original.y 從 errorViewY -> 0
// original.y 從 0 -> errorViewY
UIView.animate(withDuration: 0.2, animations: {
errorView.frame.origin.y = 0
}) { (finished) in
if finished {
UIView.animate(withDuration: 0.1, delay: 2, options: UIViewAnimationOptions.curveLinear, animations: {
errorView.frame.origin.y = errorViewY
}, completion: { (finished) in
errorView.removeFromSuperview()
errorLabel.removeFromSuperview()
})
}
}
}
沒有留言:
張貼留言