🎬 Rotation, Scaling, Translation, Image Animations & Video Playback (iOS – Xcode)
✅ 1. Definition
🔹 Object Transformations
Object transformations are techniques used to change the appearance or position of UI elements such as views or images.
They include:
- 🔄 Rotation (turning)
- 🔍 Scaling (resize)
- ➡️ Translation (moving)
🔹 Animation
Animation is the process of making UI changes smooth over time instead of instantly.
🔹 Image Array Animation
Displaying multiple images in sequence to create a frame-by-frame animation.
🔹 Video Playback
Playing video files inside an iOS app using frameworks like AVFoundation or AVKit.
🧠 2. Key Concepts
🔹 CGAffineTransform
Used to apply:
- Rotation
- Scaling
- Translation
🔹 UIView Animation
Used for smooth transitions.
🔹 UIImage Animation
Used for frame-based image sequences.
🔹 AVPlayer
Used for video playback.
🔄 3. Object Transformations
🔹 1. Translation (Move Object)
👉 Moves an object horizontally or vertically
view.transform = CGAffineTransform(translationX: 50, y: 0)
🔹 2. Scaling (Resize Object)
👉 Increases or decreases size
view.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
🔹 3. Rotation (Rotate Object)
👉 Rotates object around center
view.transform = CGAffineTransform(rotationAngle: .pi / 4)
👉 (π/4 = 45 degrees)
🔹 4. Combined Transform
view.transform =
CGAffineTransform(translationX: 50, y: 50)
.rotated(by: .pi / 4)
.scaledBy(x: 1.5, y: 1.5)
🎞️ 4. Animating Images (Image Array Animation)
🔹 Step 1: Set Image Array
imageView.animationImages = [
UIImage(named: "img1")!,
UIImage(named: "img2")!,
UIImage(named: "img3")!
]
🔹 Step 2: Set Duration
imageView.animationDuration = 1.0
🔹 Step 3: Start Animation
imageView.startAnimating()
🔹 Step 4: Stop Animation
imageView.stopAnimating()
📊 Image Animation Flow
Image1 → Image2 → Image3 → Loop (Animation)
🎬 5. Video Playback in iOS
🔹 Using AVKit (Recommended)
Step 1: Import Framework
import AVKit
import AVFoundation
🔹 Step 2: Load Video
let videoURL = URL(string: "https://example.com/video.mp4")!
let player = AVPlayer(url: videoURL)
🔹 Step 3: Create Player Controller
let playerViewController = AVPlayerViewController()
playerViewController.player = player
🔹 Step 4: Play Video
present(playerViewController, animated: true) {
player.play()
}
📊 6. Video Playback Flow
Video URL → AVPlayer → AVPlayerViewController → Play Video
💡 7. Example App
🎯 Media App
-
Image animation:
-
Video playback:
-
Object animation:
📌 8. Important Rules / Tips
- Use UIView.animate for smooth transformations
- Use UIImageView animation for frame-based effects
- Always stop animations when not needed
- Use AVKit for easy video playback
- Avoid heavy animations on main thread
⚠️ 9. Common Mistakes
- ❌ Not importing AVKit/AVFoundation
- ❌ Missing image assets for animation
- ❌ Forgetting to start animation
- ❌ Wrong rotation values (radians confusion)
- ❌ Blocking UI during video playback
🧠 10. Best Practices
- Combine animations for better UI experience
- Use vector-based transforms for performance
- Keep animations short and smooth
- Use AVPlayer for modern video apps
- Optimize image assets for animation
📝 11. Likely Exam Questions
- What is CGAffineTransform?
- Explain rotation, scaling, and translation.
- How do you animate images in iOS?
- Write code for image array animation.
- What is AVPlayer used for?
- How do you play a video in iOS?
- Differentiate image animation and video playback.
- What is UIView animation used for?
📚 12. Quick Revision Summary
-
Transformations:
- Translation → move
- Scaling → resize
- Rotation → turn
-
Image animation:
- Uses UIImageView
- Sequence of images
-
Video playback:
-
Used in:
- Games
- Media apps
- UI effects