Я пытаюсь переключить свою кнопку между воспроизведением и воспроизведением изображения при запуске и остановить тикер с помощью Swift. Мой код:Toggle Play/Pause UIBarButtonItem на панели инструментов с помощью Swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var btnPlayPause: UIBarButtonItem!
var isPlaying = false
var timer = NSTimer()
var count = 0
@IBOutlet weak var lblTime: UILabel!
@IBOutlet var myToolbar: UIToolbar!
@IBAction func btnPlay(sender: UIBarButtonItem)
{
//set the button to animate
self.myToolbar.setItems([self.btnPlayPause], animated: true)
if !isPlaying //if the ticker is not ticking
{
//change the button to a pause button
println("worked")//start the ticker
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
isPlaying = true
}else{ //if the ticker is ticking
//change the pause button to a play button
self.btnPlayPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: nil)
//pause the ticker
timer.invalidate()
//btnPlayPause.enabled = true
isPlaying = false
}
}
@IBAction func btnReset(sender: UIBarButtonItem)
{
//reset and restart the ticker
timer.invalidate()
count = 0
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
@IBAction func btnStopit(sender: UIBarButtonItem)
{
//stop and reset the ticker to "0"
timer.invalidate()
count = 0
lblTime.text = String(count)
isPlaying = false
}
func updateTime()
{
//displays ticker label with count
lblTime.text = String(count++)
}
override func viewDidLoad()
{
super.viewDidLoad()
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "btnStopit:")
self.btnPlayPause = button
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
Слушать и все остальные кнопки на панели инструментов расчищают и панель инструментов создает кнопку паузы одна сама по себе, как это:
Я хочу просто переключите мою кнопку воспроизведения с помощью кнопки «Пауза», не удаляя ни одну из других кнопок с панели инструментов. Это возможно?
Если бы кто-нибудь мог мне помочь, я бы очень признателен!
Благодаря
изменить, если isPlaying == ложь {...}, чтобы если! IsPlaying {...} –
Я ценю предложение. Однако все, что делает, делает то же самое, что написано иначе. Я изменил его, как вы предложили, и это никак не влияет на конечный результат. – Unconquered82
Если бы это решило вашу проблему, я бы опубликовал ее как ответ. Это просто правильный синтаксис для тестирования условия Bool –