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›Using Animations & Video: NSTimer class and object transformations
    Mobile Application Development 2Topic 27 of 38

    Using Animations & Video: NSTimer class and object transformations

    3 minread
    564words
    Beginnerlevel

    🎬 Using Animations & Video in iOS: NSTimer Class & Object Transformations (Xcode)


    ✅ 1. Definition

    🔹 NSTimer (Timer in iOS)

    NSTimer is a class used to execute code repeatedly or after a time interval. It is commonly used for:

    • Animations
    • Slideshow apps
    • Game loops
    • Video-like effects

    👉 Simple idea: It runs code after every fixed interval (e.g., every 1 second).


    🔹 Object Transformation

    Object transformation means changing a UI element’s:

    • Position
    • Size
    • Rotation
    • Scale (zoom in/out)
    • Opacity

    👉 It is done using CGAffineTransform and animations.


    🧠 2. Key Concepts

    🔹 Animation

    Smooth visual change of UI elements over time.

    🔹 Timer-Based Animation

    Animations controlled using NSTimer for repeated updates.

    🔹 Transform Property

    Used to apply visual changes:

    view.transform
    

    ⏱️ 3. NSTimer in iOS


    🔹 Create a Timer

    var timer: Timer?
    
    timer = Timer.scheduledTimer(timeInterval: 1.0,
                                  target: self,
                                  selector: #selector(updateUI),
                                  userInfo: nil,
                                  repeats: true)
    

    🔹 Timer Function

    @objc func updateUI() {
        print("Timer fired!")
    }
    

    🔹 Stop Timer

    timer?.invalidate()
    timer = nil
    

    🎞️ 4. Using Timer for Animation


    🔹 Example: Moving Object Automatically

    var xPosition: CGFloat = 0
    
    @objc func moveView() {
        xPosition += 10
        animatedView.frame.origin.x = xPosition
    }
    

    🔹 Timer Trigger

    timer = Timer.scheduledTimer(timeInterval: 0.1,
                                  target: self,
                                  selector: #selector(moveView),
                                  userInfo: nil,
                                  repeats: true)
    

    🔄 5. Object Transformations


    🔹 1. Translation (Move Object)

    view.transform = CGAffineTransform(translationX: 50, y: 0)
    

    🔹 2. Scaling (Zoom In/Out)

    view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
    

    🔹 3. Rotation

    view.transform = CGAffineTransform(rotationAngle: .pi / 4)
    

    👉 (π/4 = 45 degrees)


    🔹 4. Reset Transformation

    view.transform = CGAffineTransform.identity
    

    🎬 6. Animation with Transform


    🔹 Smooth Animation Example

    UIView.animate(withDuration: 1.0) {
        self.myView.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
    }
    

    🔹 Combined Transform (Move + Rotate + Scale)

    myView.transform =
        CGAffineTransform(translationX: 50, y: 50)
            .rotated(by: .pi / 4)
            .scaledBy(x: 1.5, y: 1.5)
    

    📊 7. Diagram Description (for Exams)

    Timer → triggers function repeatedly
            ↓
    Update UI (position/transform)
            ↓
    Animation effect on screen
    

    💡 8. Example App

    🎯 Moving Image App

    • Image moves left to right using timer
    • Image rotates while moving
    • Creates animation effect

    📌 9. Important Rules / Tips

    • Always invalidate timer when not needed
    • Use UIView.animate for smooth animations
    • Avoid heavy tasks inside timer
    • Use transform for efficient animations
    • Prefer modern alternatives (like GCD/animations) in advanced apps

    ⚠️ 10. Common Mistakes

    • ❌ Not stopping timer → memory leak
    • ❌ Using large intervals incorrectly
    • ❌ Overusing timer for UI updates
    • ❌ Forgetting @objc in selector methods
    • ❌ Confusing transform values

    🧠 11. Best Practices

    • Use timer only when necessary
    • Prefer UIView.animate for UI animations
    • Always stop timer in viewWillDisappear
    • Combine transformations for better effects
    • Keep animation smooth (avoid laggy updates)

    📝 12. Likely Exam Questions

    1. What is NSTimer in iOS?
    2. How do you create a timer in Swift?
    3. What is object transformation?
    4. Explain translation, rotation, and scaling.
    5. How do you stop a timer?
    6. Write code for moving a view using NSTimer.
    7. What is CGAffineTransform?
    8. Why is NSTimer used in animations?

    📚 13. Quick Revision Summary

    • NSTimer runs code repeatedly after time intervals

    • Used for animations and repeated tasks

    • Object transformation includes:

      • Move (translation)
      • Rotate
      • Scale
    • UIView.animate provides smooth animations

    • Always invalidate timer when not needed

    • Used in games, sliders, and motion effects


    Previous topic 26
    Checking for database existence and reading/displaying data
    Next topic 28
    Rotation, scaling, translation, animating image arrays, and playing video

    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 count564
      Code examples0
      DifficultyBeginner