
実機 iPhone 8 で試したのですが、バーアイテム・ボタンのどちらも問題なく `Connect`/`Disconnect` の切り替えを何度でも行えますよ。こちらで実装した `updateMotionData(deviceMotion:)` は `print("x: \(deviceMotion.gravity.x)")` するだけの単純なものですが、データの取得がちゃんと切り替わっているのは確認できました。ご提示のコード外の問題の可能性が大きいように感じます。
投稿2021/10/02 02:44
編集2021/10/04 07:50モーションセンサの@ObservedObjectの関数を
navigationBarItemsで実行すると1度だけ実行できますがが、2回目以降は実行できません。
navigationBarItemsの外側では所望動作です。
なぜでしょう?
struct ContentView: View { @ObservedObject var builtIn = MotionSensor() var body: some View { NavigationView { VStack { Button(action: { if builtIn.isStarted { builtIn.stop() }else{ builtIn.start() } }) { if builtIn.isStarted { Text("Disconnect") .foregroundColor(Color.red) .padding() }else{ Text("Connect") .padding() } } ... 中略 HStack{ Text(String(format:"%6.2f", builtIn.roll * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.pitch * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.azimuth * 180.0/Double.pi)).padding() } ... 中略 ... }.navigationBarItems(leading: Button(action: { if builtIn.isStarted { builtIn.stop() }else{ builtIn.start() } print("tapped!") }) { if builtIn.isStarted { Text("Disconnect") .foregroundColor(Color.red) .padding() }else{ Text("Connect") .padding() } }) } .navigationViewStyle(StackNavigationViewStyle())
@ObservedObjectは以下です。
import Foundation import CoreMotion class MotionSensor: NSObject, ObservableObject { @Published var isStarted = false let motionManager = CMMotionManager() func start() { // start monitoring sensor data if motionManager.isDeviceMotionAvailable { motionManager.deviceMotionUpdateInterval = 0.1 motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {(motion:CMDeviceMotion?, error:Error?) in self.updateMotionData(deviceMotion: motion!) }) } isStarted = true } func stop() { isStarted = false motionManager.stopDeviceMotionUpdates() } @Published var pitch = 0.0 @Published var roll = 0.0 @Published var azimuth = 0.0 private func updateMotionData(deviceMotion:CMDeviceMotion) { pitch = (deviceMotion.attitude.pitch) roll = (deviceMotion.attitude.roll) azimuth = (deviceMotion.attitude.yaw) }
あなたの回答
tips
プレビュー