working on my first stopwatch app.
我的第一个秒表程序。
I currently have a play button, pause button, and stop button.
我目前有一个播放按钮,暂停按钮和停止按钮。
I'd like to combine the play and pause button so that they switch back and forth.
我想合并播放和暂停按钮,以便他们来回切换。
My code looks like this:
我的代码是这样的:
var timer = NSTimer()
var count = 0
func updateTime() {
count++
time.text = "\(count)"
}
@IBAction func pauseButton(sender: AnyObject) {
timer.invalidate()
}
@IBOutlet weak var time: UILabel!
@IBAction func stopButton(sender: AnyObject) {
timer.invalidate()
count = 0
time.text = "0"
}
@IBAction func playButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
Any help is appreciated.
任何帮助都是感激。
3 个解决方案
#1
1
Try adding booleans. See my code below.
尝试添加布尔值。请参见下面的代码。
@IBOutlet weak var label: UILabel!
var time = NSTimer()
var count = 0
var running = false
func result (){
count++
label.text = String(count)
println(count)
}
@IBAction func playpause(sender: AnyObject) {
if running == false {
time = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
running = true }
else {
time.invalidate()
running = false
}
}
Hope this helps!
希望这可以帮助!
#2
0
You'll have a variable bound to the button, something like this:
将变量绑定到按钮上,如下所示:
@IBOutlet var thePlayPauseButton : UIButton!
This button will be linked with some action:
这个按钮将会链接一些动作:
@IBAction func togglePlayPauseButton (button: UIButton) {
// If we are 'paused', then play:
if button.titleLabel!.text == "Pause" {
button.titleLabel!.text = "Play"
// do actual play ...
timer = NSTimer.scheduledTimerWithTimeInterval (1,
target: self,
selector: Selector("updateTime"),
userInfo: nil,
repeats: true)
}
else if button.titleLabel!.text == "Play" {
button.titleLabel!.text = "Pause"
// do actual pause ...
timer.invalidate()
}
else { /* error */ }
}
Of course, structurally you can use a switch//case
and you can perform the toggle behavior by calling your preexisting pause
and play
methods.
当然,在结构上,您可以使用switch//case,并可以通过调用现有的pause和play方法来执行切换行为。
#3
0
I know this post is somewhat dated, but I was working with the same problem and I came up with a slightly different answer and wanted to share it to help others. Here is what I came up with in toggling the pause and play buttons.
我知道这篇文章有点过时,但我正在处理同样的问题,我想出了一个稍微不同的答案,想要分享它来帮助别人。这是我在切换暂停和播放按钮时想到的。
class ViewController: UIViewController {
var time = NSTimer()
var seconds = 0
var running = false
func timer() {
seconds++
timeLabel.text = "\(seconds)"
}
func playing() {
time = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timer"), userInfo: nil, repeats: true)
running = true
}
func pausing() {
time.invalidate()
running = false
}
@IBOutlet weak var timeLabel: UILabel!
@IBAction func stopButton(sender: AnyObject) {
time.invalidate()
seconds = 0
timeLabel.text = "0"
}
@IBAction func pausePlayToggleButton(sender: AnyObject) {
if running == false {
return playing()
} else {
return pausing()
}
}
I had both a pause and play button and I essentially took their effects and placed them in functions and used them as return values for a single button.
我有一个暂停和播放按钮,我把它们的效果放在函数中,并把它们作为单个按钮的返回值。
#1
1
Try adding booleans. See my code below.
尝试添加布尔值。请参见下面的代码。
@IBOutlet weak var label: UILabel!
var time = NSTimer()
var count = 0
var running = false
func result (){
count++
label.text = String(count)
println(count)
}
@IBAction func playpause(sender: AnyObject) {
if running == false {
time = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
running = true }
else {
time.invalidate()
running = false
}
}
Hope this helps!
希望这可以帮助!
#2
0
You'll have a variable bound to the button, something like this:
将变量绑定到按钮上,如下所示:
@IBOutlet var thePlayPauseButton : UIButton!
This button will be linked with some action:
这个按钮将会链接一些动作:
@IBAction func togglePlayPauseButton (button: UIButton) {
// If we are 'paused', then play:
if button.titleLabel!.text == "Pause" {
button.titleLabel!.text = "Play"
// do actual play ...
timer = NSTimer.scheduledTimerWithTimeInterval (1,
target: self,
selector: Selector("updateTime"),
userInfo: nil,
repeats: true)
}
else if button.titleLabel!.text == "Play" {
button.titleLabel!.text = "Pause"
// do actual pause ...
timer.invalidate()
}
else { /* error */ }
}
Of course, structurally you can use a switch//case
and you can perform the toggle behavior by calling your preexisting pause
and play
methods.
当然,在结构上,您可以使用switch//case,并可以通过调用现有的pause和play方法来执行切换行为。
#3
0
I know this post is somewhat dated, but I was working with the same problem and I came up with a slightly different answer and wanted to share it to help others. Here is what I came up with in toggling the pause and play buttons.
我知道这篇文章有点过时,但我正在处理同样的问题,我想出了一个稍微不同的答案,想要分享它来帮助别人。这是我在切换暂停和播放按钮时想到的。
class ViewController: UIViewController {
var time = NSTimer()
var seconds = 0
var running = false
func timer() {
seconds++
timeLabel.text = "\(seconds)"
}
func playing() {
time = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timer"), userInfo: nil, repeats: true)
running = true
}
func pausing() {
time.invalidate()
running = false
}
@IBOutlet weak var timeLabel: UILabel!
@IBAction func stopButton(sender: AnyObject) {
time.invalidate()
seconds = 0
timeLabel.text = "0"
}
@IBAction func pausePlayToggleButton(sender: AnyObject) {
if running == false {
return playing()
} else {
return pausing()
}
}
I had both a pause and play button and I essentially took their effects and placed them in functions and used them as return values for a single button.
我有一个暂停和播放按钮,我把它们的效果放在函数中,并把它们作为单个按钮的返回值。