import SwiftUI
import CoreMotion
struct ContentView: View {
@State private var message = "Mueve tu mano"
let motionManager = CMMotionManager()
var body: some View {
VStack {
Text(message)
.font(.title)
Spacer()
}
.onAppear {
startMotionUpdates()
}
}
func startMotionUpdates() {
if motionManager.isAccelerometerAvailable {
motionManager.accelerometerUpdateInterval = 0.1
motionManager.startAccelerometerUpdates(to: .main) { data, error in
guard let acceleration = data?.acceleration else { return }
detectHandMovement(acceleration)
}
}
}
func detectHandMovement(_ acceleration: CMAcceleration) {
let threshold = 1.0 // Ajusta el umbral según sea necesario
if acceleration.x > threshold {
message = "Movimiento a la derecha"
} else if acceleration.x < -threshold {
message = "Movimiento a la izquierda"
} else if acceleration.y > threshold {
message = "Mano hacia arriba"
} else if acceleration.y < -threshold {
message = "Mano hacia abajo"
}
}
}
@main
struct WatchApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Publicado el día 4 de enero de 2024CATEGORÍAS
Swift, Xcode