gpt4 book ai didi

Swift - 如何使用 UIBarButtonItem 在播放和暂停之间切换

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:40 24 4
gpt4 key购买 nike

我正在我的计时器应用程序中尝试使用以下 Swift 代码来使用我拥有的相同按钮启动和暂停。但它不会切换。该按钮在 IDE 中的标识符设置为“播放”。当我运行该应用程序时,计时器会正确启动和暂停。但它没有切换按钮。它始终保持为“播放”

@IBAction func pressPausePlay(sender: AnyObject) {

if playPause == false
{
timer.invalidate()
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "pressPausePlay")

playPause = true
}
else
{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pressPausePlay")

playPause = false
}

}

最佳答案

您可以使用此示例方案在播放和暂停之间切换,用于按钮标题、图像、导航栏、工具栏

import UIKit
import AVFoundation


class ViewController: UIViewController {

var myPlayer = AVAudioPlayer()


@IBOutlet var myNaviBar: UINavigationBar! //OUTLET TO NAVIGATION BAR IMPORTANT TO WORK WITH ITS BARBUTTON ITEMS

@IBOutlet var myToolBar: UIToolbar!


//**************START :Bottom LEFT TOOLBAR Bar Button Item***********/
@IBAction func toolBarItemPlay(sender: AnyObject) {

myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "toolBarItemPause:")], animated: true)
myPlayer.play()
}


@IBAction func toolBarItemPause(sender: AnyObject) {

myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "toolBarItemPlay:")], animated: true)
myPlayer.pause()
}
//**************END :Bottom LEFT TOOLBAR Bar Button Item***********/




//**************START :Top LEFT Naivation Bar Button Item***********/

@IBAction func leftNavgationbarPlay(sender: UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "leftNavigationbarPause:")
myPlayer.play()
}

//no need of control drag to this one below because we are calling from the above leftNavgationbarPlay's Selector/action
@IBAction func leftNavigationbarPause(sender : UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "leftNavgationbarPlay:")
myPlayer.pause()
}
//**************END :Top LEFT Naivation Bar Button Item***********/




//**************START :Top Right Naivation Bar Button Item***********/
@IBAction func naviPlay(sender: UIBarButtonItem) {
//change the topright navigation bar item from SystemItem Play '>' to systemitem pause '||'
// here I am assigning the top left item Play to Bar button item Pause

/* ACTION SELECTOR IMPORTANT. IT MUST CALL THE IB ACTION FUNCTION OF PAUSE AND VICEVERSA, WITH ":" */
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "naviPause:")
myPlayer.play()
}

@IBAction func naviPause(sender: UIBarButtonItem) {
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "naviPlay:")
myPlayer.pause()
}
//**************END : Top Right Naivation Bar Button Item***********/





//************START :BUTTON IMAGE CHANGE - ">"to "||"************//

@IBAction func imagePlayButton(sender: UIButton) {
let chkImage = sender.currentImage!
if chkImage == UIImage(named: "play.png"){


sender.setImage(UIImage(named: "pause.png"), forState: .Normal)

myPlayer.play()
} else {
sender.setImage(UIImage(named: "play.png"), forState: .Normal)
myPlayer.pause()
}

}

//************END :BUTTON IMAGE CHANGE - ">"to "||"************//






//************START BUTTON TITLE CHANGE - PLAY to PAUSE************//

@IBAction func playButton(sender: UIButton) {
let chkTitle = sender.currentTitle
if chkTitle == "Play" {
sender.setTitle("Pause", forState: .Normal)
myPlayer.play()
} else {
sender.setTitle("Play", forState: .Normal)
myPlayer.pause()
}

}

//************END BUTTON TITLE CHANGE - PLAY to PAUSE************//




@IBAction func naviStop(sender: UIBarButtonItem) {

myPlayer.stop()
myPlayer.currentTime = 0

}

@IBOutlet var sliderUI: UISlider!


@IBAction func mySlider(sender: UISlider) {


myPlayer.volume = sliderUI.value
}




override func viewDidLoad() {
super.viewDidLoad()


let audioFilePath = NSBundle.mainBundle().pathForResource("ARRehman", ofType: "mp3")

//need NSURL from string

do {
myPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioFilePath!))

}
catch {
print("Some error")

}

}

}

关于Swift - 如何使用 UIBarButtonItem 在播放和暂停之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com