It means reading motion sensor values (accelerometer/gyroscope) and displaying them in the app UI or console.
👉 Example:
The Shake API detects when a user physically shakes the iPhone/iPad.
👉 Used for:
Used to access:
iOS provides built-in methods for shake detection:
motionBeganmotionEndedmotionCancelledimport CoreMotion
let motionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.2
motionManager.startAccelerometerUpdates(to: OperationQueue.main) { data, error in
if let acceleration = data?.acceleration {
print("X: \(acceleration.x)")
print("Y: \(acceleration.y)")
print("Z: \(acceleration.z)")
}
}
xLabel.text = "X: \(acceleration.x)"
yLabel.text = "Y: \(acceleration.y)"
zLabel.text = "Z: \(acceleration.z)"
Device Movement
↓
Accelerometer / Gyroscope
↓
CMMotionManager
↓
Swift Code (Output Data)
↓
UI Labels / Console
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
becomeFirstResponder()
}
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken!")
}
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
label.text = "Shaken!"
view.backgroundColor = .random()
}
}
User Shakes Device
↓
motionBegan()
↓
motionEnded()
↓
App Executes Action
↓
UI Updates (Label/Image/Color)
becomeFirstResponder()motionBegan method?Sensor data = accelerometer + gyroscope values
Output = display data in UI or console
Shake API detects device shake motion
Uses:
motionBeganmotionEndedRequires becomeFirstResponder()
Used in:
Open this section to load past papers