美文网首页
一个UIView 扩展,飘一个文字 然后消失,本意是用在btn点

一个UIView 扩展,飘一个文字 然后消失,本意是用在btn点

作者: 格调main | 来源:发表于2016-03-15 15:25 被阅读35次

想做一个btn 点击飘一个红色+1 然后消失, 就自己写了个函数实现 如下
~

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()
    }
    

}

}
~

相关文章

网友评论

      本文标题:一个UIView 扩展,飘一个文字 然后消失,本意是用在btn点

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