【发布时间】:2015-10-18 10:12:50
【问题描述】:
我目前正在学习 Swift 和 iOS 编程。我学会了创建一个显示秒表的秒表应用程序。我目前正在尝试将秒表的开始和停止按钮结合起来。这意味着当应用程序加载时,它会有一个开始按钮,但是当我按下开始按钮时,它会变成一个停止按钮,当我按下计时器停止并且按钮变为开始按钮时。我已经搜索了互联网和堆栈溢出,但我无法找到与我想要的类似的东西。我当前的代码附在下面!
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var count = 0.0
func updateTime() {
count = count + 0.1
label.text = "\(count)"
}
@IBOutlet weak var label: UILabel!
@IBAction func start(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: ("updateTime"), userInfo: nil, repeats: true)
}
@IBAction func pause(sender: AnyObject) {
timer.invalidate()
}
@IBAction func reset(sender: AnyObject) {
timer.invalidate()
count = 0
label.text = "0"
}
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.
}
}
【问题讨论】:
-
您在按钮上使用任何图像还是仅使用文本?
-
哇!那很快。我正在使用一种叫做条形按钮项的东西
-
您是否在使用导航栏或工具栏内的栏按钮项?这实际上很重要,因为答案比使用标准按钮稍微复杂一些(但仍然没有什么复杂的)。
标签: ios swift button timer stopwatch