
An accelerometer is a sensor in iOS devices that measures linear motion and acceleration of the device in 3D space.
An accelerometer is a sensor in iOS devices that measures linear motion and acceleration of the device in 3D space.
👉 It tells:
A gyroscope measures rotation and angular velocity of the device.
👉 It tells:
👉 Simple difference:
iOS framework used to access motion sensors:
Main class used to access motion data.
Combines:
import CoreMotion
let motionManager = CMMotionManager()
if motionManager.isAccelerometerAvailable {
print("Accelerometer available")
}
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)")
}
}
| Axis | Meaning |
|---|---|
| X | Left / Right movement |
| Y | Up / Down movement |
| Z | Forward / Back movement |
motionManager.gyroUpdateInterval = 0.2
motionManager.startGyroUpdates(to: OperationQueue.main) { data, error in
if let rotation = data?.rotationRate {
print("X Rotation: \(rotation.x)")
print("Y Rotation: \(rotation.y)")
print("Z Rotation: \(rotation.z)")
}
}
| Axis | Meaning |
|---|---|
| X | Tilt forward/back |
| Y | Tilt left/right |
| Z | Spin rotation |
Device Movement
↓
Sensors (Accelerometer + Gyroscope)
↓
CMMotionManager
↓
Motion Data (X, Y, Z)
↓
iOS App UI Update
motionManager.stopAccelerometerUpdates()
motionManager.stopGyroUpdates()
Accelerometer → measures movement (X, Y, Z)
Gyroscope → measures rotation
Core Motion framework is used
Main class: CMMotionManager
Used in:
Always stop updates to save battery
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.