如何在Swift中将播放按钮切换为暂停按钮?

如何在Swift中将播放按钮切换为暂停按钮?,swift,Swift,正在开发我的第一个秒表应用程序 我目前有一个播放按钮、暂停按钮和停止按钮 我想将“播放”和“暂停”按钮组合起来,以便它们来回切换 我的代码如下所示: var timer = NSTimer() var count = 0 func updateTime() { count++ time.text = "\(count)" } @IBAction func pauseButton(sender: AnyObject) { timer.invalidate()

正在开发我的第一个秒表应用程序

我目前有一个播放按钮、暂停按钮和停止按钮

我想将“播放”和“暂停”按钮组合起来,以便它们来回切换

我的代码如下所示:

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)


}
@IBOutlet var thePlayPauseButton : UIButton!

非常感谢您的帮助。

按钮将绑定一个变量,如下所示:

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)


}
@IBOutlet var thePlayPauseButton : UIButton!
此按钮将与某些操作链接:

@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 */ }
}

当然,在结构上,您可以使用
开关//case
,您可以通过调用先前存在的
暂停
播放
方法来执行切换行为。

尝试添加布尔值。请参阅下面的代码

 @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

        }



    }

希望这有帮助

我知道这篇文章有点过时,但我也遇到了同样的问题,我想出了一个稍微不同的答案,希望与大家分享,以帮助他人。下面是我在切换暂停和播放按钮时想到的

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()
    }
}
我有一个暂停和播放按钮,我基本上采取了它们的效果,并把它们放在函数中,用它们作为单个按钮的返回值