如何在 Swift2 中使用标签进行计数
How to count with a label in Swift2
我有 1 个按钮和 1 个标签。当我点击按钮时,标签应该数到数字末尾。现在它立即显示最终结果。我怎样才能让它之间的每个数字也显示出来?是否存在这样做的功能(开始并计数到具有特殊效果的数字)?
谢谢
var start = 0
var end = 10
@IBAction func Button1(sender: UIButton) {
while start <= end {
Label1.text = "\(start)"
start = start + 1
}
使用这个,
import UIKit
class ViewController: UIViewController {
var start = 0
var end = 10
var timer = NSTimer()
func updateTime() {
if start == end {
timer.invalidate()
} else {
start++
Label1.text = "\(start)"
}
}
@IBOutlet weak var Label1: UILabel!
@IBAction func button(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
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.
}
}
您可以通过将当前设置的 NSTimer 间隔更改为 0.5 来更改速度,设置任何您想要的
我有 1 个按钮和 1 个标签。当我点击按钮时,标签应该数到数字末尾。现在它立即显示最终结果。我怎样才能让它之间的每个数字也显示出来?是否存在这样做的功能(开始并计数到具有特殊效果的数字)?
谢谢
var start = 0
var end = 10
@IBAction func Button1(sender: UIButton) {
while start <= end {
Label1.text = "\(start)"
start = start + 1
}
使用这个,
import UIKit
class ViewController: UIViewController {
var start = 0
var end = 10
var timer = NSTimer()
func updateTime() {
if start == end {
timer.invalidate()
} else {
start++
Label1.text = "\(start)"
}
}
@IBOutlet weak var Label1: UILabel!
@IBAction func button(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
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.
}
}
您可以通过将当前设置的 NSTimer 间隔更改为 0.5 来更改速度,设置任何您想要的