如何在Swift中使用CoreAnimation为UILabel文本属性设置动画?

时间:2016-04-11 08:48:20

标签: ios swift core-animation uiviewanimation cabasicanimation

我想使用具有缓动函数的CoreAnimation(例如easeInOutQuad)将UILabel.text值从0增加到100。但似乎文本属性不可动画。那么如何在CA的帮助下实现这一目标呢?或者我是否需要自己实现缓动功能并使用GCD调用它? 感谢

P.S。我想尽可能地坚持使用CA.

1 个答案:

答案 0 :(得分:3)

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    var counter = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("animate"), userInfo: nil, repeats: true)
        timer.fire()
    }

    func animate() {
        UIView.transitionWithView(label,
                              duration: 1.0,
                              options: [.CurveEaseInOut],
                              animations: { () -> Void in
                                self.counter += 1
                                self.label.text = "\(self.counter)"
        }, completion: nil)

    }
}