📱 Working with the Accelerometer & Gyroscope (iOS – Core Motion)
✅ 1. Definition
🔹 Accelerometer
An accelerometer is a sensor in iOS devices that measures linear motion and acceleration of the device in 3D space.
👉 It tells:
- How fast the device is moving
- Direction of movement
- Tilt changes
🔹 Gyroscope
A gyroscope measures rotation and angular velocity of the device.
👉 It tells:
- How the device is rotating
- Orientation changes (spin, twist)
👉 Simple difference:
- 📍 Accelerometer → movement (straight motion)
- 🔄 Gyroscope → rotation (turning motion)
🧠 2. Key Concepts
🔹 Core Motion Framework
iOS framework used to access motion sensors:
- Accelerometer
- Gyroscope
- Device motion (combined data)
🔹 CMMotionManager
Main class used to access motion data.
🔹 Device Motion
Combines:
- Accelerometer
- Gyroscope
- Magnetometer
⚙️ 3. Import Framework
import CoreMotion
📦 4. Setup Motion Manager
🔹 Create Object
let motionManager = CMMotionManager()
🔹 Check Availability
if motionManager.isAccelerometerAvailable {
print("Accelerometer available")
}
📊 5. Using Accelerometer
🔹 Start Accelerometer Updates
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)")
}
}
📍 Accelerometer Axis
| Axis |
Meaning |
| X |
Left / Right movement |
| Y |
Up / Down movement |
| Z |
Forward / Back movement |
🔄 6. Using Gyroscope
🔹 Start Gyroscope Updates
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)")
}
}
📍 Gyroscope Axis
| Axis |
Meaning |
| X |
Tilt forward/back |
| Y |
Tilt left/right |
| Z |
Spin rotation |
📊 7. Motion Data Flow Diagram
Device Movement
↓
Sensors (Accelerometer + Gyroscope)
↓
CMMotionManager
↓
Motion Data (X, Y, Z)
↓
iOS App UI Update
🎮 8. Example App
🎯 Motion-Control Game
- Tilt phone → move character
- Rotate device → change direction
- Shake device → trigger action
📌 9. Important Rules / Tips
- Always check sensor availability
- Set update interval carefully (battery saving)
- Use main queue for UI updates
- Stop updates when not needed
- Combine accelerometer + gyroscope for accuracy
🔴 Stop Updates (Important)
motionManager.stopAccelerometerUpdates()
motionManager.stopGyroUpdates()
⚠️ 10. Common Mistakes
- ❌ Not stopping motion updates → battery drain
- ❌ Using too fast update interval
- ❌ Not checking availability
- ❌ Updating UI on background thread
- ❌ Confusing acceleration vs rotation
🧠 11. Best Practices
- Use low update frequency for efficiency
- Combine both sensors for better accuracy
- Always stop updates when screen is inactive
- Use motion data for games and fitness apps
- Test on real devices only
📝 12. Likely Exam Questions
- What is an accelerometer in iOS?
- What is the use of gyroscope?
- Differentiate accelerometer and gyroscope.
- What is Core Motion framework?
- Write code to access accelerometer data.
- What is CMMotionManager used for?
- Why are motion sensors used in apps?
- Explain motion data flow with diagram.
📚 13. Quick Revision Summary
-
Accelerometer → measures movement (X, Y, Z)
-
Gyroscope → measures rotation
-
Core Motion framework is used
-
Main class: CMMotionManager
-
Used in:
- Games
- Fitness apps
- Navigation apps
-
Always stop updates to save battery