【问题标题】:How do I create a loop for this action in Swift?如何在 Swift 中为此操作创建循环?
【发布时间】:2021-03-10 19:44:28
【问题描述】:

我在 xcode 中制作了一个通过按钮振动的应用程序,但我想在振动期间添加一定持续时间的间隙。我正在使用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate),我想在振动发生时添加间隙。然后添加哔声 3 秒并使用循环重复此操作。这就是我到目前为止所拥有的。如何创建循环?我正在使用 iphone 7 plus 进行测试。

class ViewController: UIViewController {
    var audioPlayer = AVAudioPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let sound = Bundle.main.path(forResource: "beep", ofType: "mp3")
        do{
            // initialized it with URL created above
            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
        }
        catch{
            print(error)
    }

    }
    @IBAction func PressMe(_ sender: Any) {
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        for _ in 1...6 {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
            Thread.sleep(forTimeInterval: 0.002)
            let seconds = 1.0
            DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
                self.audioPlayer.play()
            }
            
    }
    }
}

【问题讨论】:

    标签: swift iphone xcode xcode12


    【解决方案1】:

    就是这样..

    play
      play a sound
      timer 2 seconds call gap
    
    gap 
      stop playing the sound
      timer 2 seconds call play
    

    【讨论】:

    • 实际代码是什么?能不能把代码写出来
    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多