ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4126Accelerometer Gyroscop…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4126

    Mobile Application Development 2

    Progress0 / 38 done
    Topics38
    1Understanding Xcode2Using the Xcode interface builder and objects library3Understanding view hierarchy and creating a custom app icon
    4Understanding outlets and actions5Using text fields, buttons, labels, web views, and page controllers6Using views with subviews and creating views using code
    7Working with the single view template8Exploring the app delegate and adding new view controllers9Transitioning between multiple view controllers using animations
    10Tabbar and master detail templates
    11Customizing for different inputs12Adjusting text field behaviors and dismissing the keyboard13Detecting keyboard activities with notification center14Using scroll view and responding to keyboard activities programmatically
    15Detecting device hardware16Dynamically adjusting graphical layouts and creating universal apps
    17Understanding UITableView and UITableViewCell18Working with UITableView data source and delegate19Master detail template, drill-down menus, and navigation20Using property lists for data persistence and creating multi-section tables
    21Portrait and landscape modes22Handling device rotation and forcing specific orientation23Dynamically adjusting layouts based on rotation
    24Importing sqlite3 and creating a database25Writing tables, inserting records, and bundling a database with your app26Checking for database existence and reading/displaying data
    27NSTimer class and object transformations28Rotation, scaling, translation, animating image arrays, and playing video
    29Email, Safari, and SMS30Working with camera and photo library
    31Consuming and parsing XML and JSON32Integrating Twitter and Facebook with iOS apps
    33MapKit and MKMapView34Getting and displaying user location and directional information35Displaying map annotations, disclosure buttons, and reverse geocoding36Working with iCloud
    37Gyroscope and accelerometer38Outputting sensor data and using the Shake API
    3 min read
    Topic 37 of 38
    Topic Study Note Illustration
    37

    Working with the Accelerometer: Gyroscope and accelerometer

    An accelerometer is a sensor in iOS devices that measures linear motion and acceleration of the device in 3D space.

    📱 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

    1. What is an accelerometer in iOS?
    2. What is the use of gyroscope?
    3. Differentiate accelerometer and gyroscope.
    4. What is Core Motion framework?
    5. Write code to access accelerometer data.
    6. What is CMMotionManager used for?
    7. Why are motion sensors used in apps?
    8. 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


    ← Previous topic 36
    Working with iCloud
    Next topic 38 →
    Outputting sensor data and using the Shake API

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    529words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2