我希望保持UISlider的状态,然后在视图控制器之间进行更改,并使UISlider在另一个视图控制器中工作(现在它只在UISlider的当前视图控制器中工作)
我是Swift的初学者,通常是Xcode。提前感谢您的时间和帮助:)
首先,我有一个带有音乐课程的视图控制器:
class MusicHelper {
let defaults = UserDefaults.standard
static let sharedHelper = MusicHelper()
var musicBackgroundIntro:AVAudioPlayer = AVAudioPlayer()
func playBackgroundMusic() {
do {
let audioPath = Bundle.main.path(forResource: "Music", ofType: "mp3")
try musicBackgroundIntro = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
musicBackgroundIntro.numberOfLoops = -1
musicBackgroundIntro.prepareToPlay()
musicBackgroundIntro.play()
} catch {
print("Cannot play the file")
}
}
func stopBackgroundMusic() {
musicBackgroundIntro.stop()
}
其次,我有另一个带有UISlider的View Controller:
@IBOutlet weak var SliderMusic: UISlider!
@IBAction func checkStateSliderMusic(_ sender: UISlider) {
MusicHelper.sharedHelper.musicBackgroundIntro.volume = sender.value
}
知道如何在其他View Controller中应用UISlider并保持状态(修改音量)?
答案 0 :(得分:0)
当视图控制器加载时,您可以根据当前音量值设置滑块值。在viewDidLoad
UIViewController
方法中设置滑块的值。
示例:
override func viewDidLoad() {
super.viewDidLoad()
musicSlider.value = Float(MusicHelper.sharedHelper.musicBackgroundIntro.volume)
}