ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development 2
    COMP4126
    Progress0 / 38 topics
    Topics
    1. Creating an iOS App: Understanding Xcode2. Using the Xcode interface builder and objects library3. Understanding view hierarchy and creating a custom app icon4. Outlets, Actions, and Views: Understanding outlets and actions5. Using text fields, buttons, labels, web views, and page controllers6. Using views with subviews and creating views using code7. Using View Controllers: Working with the single view template8. Exploring the app delegate and adding new view controllers9. Transitioning between multiple view controllers using animations10. Application Templates: Tabbar and master detail templates11. The iOS Keyboard: Customizing for different inputs12. Adjusting text field behaviors and dismissing the keyboard13. Detecting keyboard activities with notification center14. Using scroll view and responding to keyboard activities programmatically15. Working with Different iOS Devices (iPhone & iPad): Detecting device hardware16. Dynamically adjusting graphical layouts and creating universal apps17. Using Table Views: Understanding UITableView and UITableViewCell18. Working with UITableView data source and delegate19. Master detail template, drill-down menus, and navigation20. Using property lists for data persistence and creating multi-section tables21. Supporting Screen Rotations: Portrait and landscape modes22. Handling device rotation and forcing specific orientation23. Dynamically adjusting layouts based on rotation24. Working with Databases: Importing sqlite3 and creating a database25. Writing tables, inserting records, and bundling a database with your app26. Checking for database existence and reading/displaying data27. Using Animations & Video: NSTimer class and object transformations28. Rotation, scaling, translation, animating image arrays, and playing video29. Accessing Integrated iOS Apps: Email, Safari, and SMS30. Working with camera and photo library31. Using Web Services: Consuming and parsing XML and JSON32. Integrating Twitter and Facebook with iOS apps33. Working with iOS Maps and Location Services: MapKit and MKMapView34. Getting and displaying user location and directional information35. Displaying map annotations, disclosure buttons, and reverse geocoding36. Working with iCloud37. Working with the Accelerometer: Gyroscope and accelerometer38. Outputting sensor data and using the Shake API
    COMP4126›Working with the Accelerometer: Gyroscope and accelerometer
    Mobile Application Development 2Topic 37 of 38

    Working with the Accelerometer: Gyroscope and accelerometer

    3 minread
    529words
    Beginnerlevel

    📱 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count529
      Code examples0
      DifficultyBeginner