想做一個(gè)btn 點(diǎn)擊飄一個(gè)紅色+1 然后消失, 就自己寫了個(gè)函數(shù)實(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
// 完成動(dòng)畫刪除
textLabel.removeFromSuperview()
}
}
}
~