1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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() } } } |