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 PortalCOMP4126Animations Nstimer Traโ€ฆ
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 27 of 38
    Topic Study Note Illustration
    27

    Using Animations & Video: NSTimer class and object transformations

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

    ๐ŸŽฌ 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

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

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2