2016-04-16 58 views
-1

我有一个UIPageViewController,其中每个VC的中心都有一个数字。向上/向下数字动画计数

我想,当我从视图中查看刷卡,数量将在0开始和计数,直到它得到正确的号码(或者,如果数字为负 - 倒计时)就像这个GIF:

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

我该怎么做?

谢谢!

+1

https://github.com/dataxpress/UICountingLabel 试试这个 –

+0

@ khuong291我已经尝试了'while'语句添加到标签1每一次,但没有奏效 –

+0

@matiboo谢谢!那就是我正在搜索的内容 –

回答

1

您可以使用NSTimer来实现这一点。

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

enter image description here

然后做像这样:

这样创建布局

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

它将工作。

+0

您需要在完成该操作后使该NSTimer无效 – Hamish

+0

为什么?如果我不使它无效,在这种情况下会发生什么? – Khuong

+0

它将继续被运行循环调用,这是毫无意义和浪费的。此外,如果多次调用'startButtonTapped',则会在运行循环中添加多个定时器,这也会造成浪费,并可能导致不希望的结果。 – Hamish