检测何时进行了斩波动作-Swift

问题描述

我从我的第一个iOS应用程序开始,当您轻弹手机时,我试图获取陀螺仪数据来播放鞭子声。

据我所知,我应该使用CoreMotion来获取陀螺仪的状态,然后做一些数学运算以得出类似鞭子的手势,然后运行我的功能?

这是我到目前为止所拥有的-这是我的ContentView.swift文件。

import SwiftUI
import AVFoundation
import CoreMotion

let popSound = Bundle.main.url(forResource: "whip",withExtension: "mp3")
    var audioPlayer = AVAudioPlayer()

var motionManager: CMMotionManager!

func audioPlayback() {
    do {
         audioPlayer = try AVAudioPlayer(contentsOf: popSound!)
         audioPlayer.play()
     } catch {
         print("couldn't load sound file")
     }
}

struct ContentView: View {
    var body: some View {
        Text("Press button!")
        Button(action: {
            audioPlayback()
        },label: {
            Text("Press me!")
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
            ContentView()
    }
}

当前设置为按钮。有人可以将我链接到资源,还是可以通过它浏览我?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)