想做一個btn 點擊飄一個紅色+1 然后消失, 就自己寫了個函數(shù)實現(xiàn) 如下
~
extension UIView{
func driftingText(text:String,color:UIColor = UIColor.redColor()){
let textNum:CGFloat = CGFloat(text.characters.count)
UIFont.systemFontSize()
let textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: UIFont.systemFontSize()*textNum, height: 20))
textLabel.text = text
textLabel.textColor = color
textLabel.x = self.bounds.width/2
textLabel.y = 0
self.addSubview(textLabel)
UIView.transitionWithView(textLabel, duration: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
textLabel.y = -20
textLabel.alpha = 0
}) { (bool) -> Void in
// 完成動畫刪除
textLabel.removeFromSuperview()
}
}
}
~