计数上/下数字动画

我有一个UIPageViewController ,其中每个VC的中心都有一个数字。

我希望当我从视图滑动到视图时,数字将从0开始计数,直到达到正确的数字(或者如果数字为负数 – 倒数),就像在这个gif中一样:

https://d13yacurqjgara.cloudfront.net/users/345970/screenshots/2126044/shot.gif

我怎样才能做到这一点?

谢谢!

您可以使用NSTimer来实现此目的。

这是我为您创建的示例项目。

像这样创建布局:

在此处输入图像描述

然后在你的ViewController这样做:

 import UIKit class ViewController: UIViewController { @IBOutlet var countingLabel: UILabel! var number = 0 var destinationNumber = 30 var timer: NSTimer! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func startButtonTapped(sender: AnyObject) { timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "countUp", userInfo: nil, repeats: true) } func countUp() { if number < destinationNumber { number += 1 countingLabel.text = "\(number)" } else { timer.invalidate() } } } 

它会工作。