import SwiftUI
import CoreMotion
struct ContentView: View {
@State private var isRaisingHand = false
let motion = CMMotionManager()
var body: some View {
VStack {
Text("Levantando la mano: \(isRaisingHand ? "Sí" : "No")")
.font(.headline)
.padding()
Spacer()
}
.onAppear {
startDetection()
}
.onDisappear {
stopDetection()
}
}
func startDetection() {
if motion.isDeviceMotionAvailable {
motion.deviceMotionUpdateInterval = 0.1
motion.startDeviceMotionUpdates(to: OperationQueue.current!) { data, _ in
if let attitude = data?.attitude {
if attitude.roll > 0.2 {
// El brazo está levantado hacia arriba
isRaisingHand = true
} else {
// El brazo está en otra posición
isRaisingHand = false
}
}
}
}
}
func stopDetection() {
motion.stopDeviceMotionUpdates()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@main
struct pruebaApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Publicado el día 5 de enero de 2024CATEGORÍAS
Swift, Xcode