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

想做一个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()
    }
    

}

}
~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容