Bird
Raised Fist0
iOS Swiftmobile~20 mins

Transition effects in iOS Swift - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Color Transition Screen
This screen shows a button that, when tapped, smoothly changes the background color with a fade transition effect.
Target UI
-------------------------
|                       |
|                       |
|     [Change Color]     |
|                       |
|                       |
-------------------------
Add a UIButton centered on the screen with the title 'Change Color'.
When the button is tapped, the background color of the view changes to a random color.
The color change should use a smooth fade transition effect lasting about 0.5 seconds.
Starter Code
iOS Swift
import UIKit

class ColorTransitionViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        let button = UIButton(type: .system)
        button.setTitle("Change Color", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(button)

        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        // TODO: Add target action for button tap
    }

    // TODO: Add function to handle color change with transition
}
Task 1
Task 2
Solution
iOS Swift
import UIKit

class ColorTransitionViewController: UIViewController {

    private let button = UIButton(type: .system)

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        button.setTitle("Change Color", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(button)

        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        button.addTarget(self, action: #selector(changeColor), for: .touchUpInside)
    }

    @objc private func changeColor() {
        let newColor = UIColor(
            red: CGFloat.random(in: 0...1),
            green: CGFloat.random(in: 0...1),
            blue: CGFloat.random(in: 0...1),
            alpha: 1.0
        )

        UIView.transition(with: view, duration: 0.5, options: [.transitionCrossDissolve], animations: {
            self.view.backgroundColor = newColor
        }, completion: nil)
    }
}

We create a UIButton centered on the screen with the title "Change Color". We add a target action to the button that calls the changeColor method when tapped. In changeColor, we generate a random color and then use UIView.transition with the transitionCrossDissolve option to smoothly fade the background color change over 0.5 seconds. This creates a nice visual transition effect for the color change.

Final Result
Completed Screen
-------------------------
|                       |
|                       |
|     [Change Color]     |
|                       |
|                       |
-------------------------
When the user taps the 'Change Color' button, the background color smoothly fades to a new random color over half a second.
Stretch Goal
Add a button to toggle between fade and slide transition effects for the background color change.
💡 Hint
Use UIView.transition options like .transitionFlipFromLeft or .transitionCurlUp for different animations.

Practice

(1/5)
1. What is the main purpose of using CATransition in iOS Swift development?
easy
A. To manage data storage
B. To handle user input events
C. To perform network requests
D. To create custom animations when switching between views

Solution

  1. Step 1: Understand the role of CATransition

    CATransition is a Core Animation class used to animate changes between views or layers.
  2. Step 2: Identify its purpose in UI

    It helps create smooth visual effects when switching screens or views, improving user experience.
  3. Final Answer:

    To create custom animations when switching between views -> Option D
  4. Quick Check:

    CATransition = Custom view animations [OK]
Hint: CATransition = animation for view changes [OK]
Common Mistakes:
  • Confusing CATransition with data handling classes
  • Thinking it manages user input
  • Assuming it handles networking
2. Which of the following is the correct way to set the transition type to a fade effect using CATransition in Swift?
easy
A. transition.type = CATransitionType.fade
B. transition.type = .fade
C. transition.type = "fade"
D. transition.type = Fade

Solution

  1. Step 1: Recall CATransition type syntax

    In Swift, CATransition.type is a String property. The value for a fade effect is "fade".
  2. Step 2: Check each option

    A: CATransitionType.fade - no such type exists. B: .fade - invalid syntax. D: Fade - undefined. C is correct.
  3. Final Answer:

    transition.type = "fade" -> Option C
  4. Quick Check:

    Enum cases use dot syntax = "fade" [OK]
Hint: Use string "fade" for fade transition [OK]
Common Mistakes:
  • Using non-existent CATransitionType.fade
  • Using invalid dot syntax .fade
  • Using capitalized undefined Fade
3. What will be the visible effect when running this Swift code snippet?
let transition = CATransition()
transition.type = "push"
transition.subtype = "fromRight"
view.layer.add(transition, forKey: "transition")
view.backgroundColor = .red
medium
A. The view background color changes instantly with no animation
B. The view background color changes with a push animation from the right
C. The view background color fades in smoothly
D. The app crashes due to invalid transition subtype

Solution

  1. Step 1: Analyze transition type and subtype

    The transition type is "push" and subtype is "fromRight", meaning the new content will push in from the right side.
  2. Step 2: Understand effect on view background color

    Adding the transition to the layer and then changing backgroundColor triggers the push animation showing the color change.
  3. Final Answer:

    The view background color changes with a push animation from the right -> Option B
  4. Quick Check:

    Push + fromRight = push animation from right [OK]
Hint: Push + fromRight means slide in from right [OK]
Common Mistakes:
  • Expecting fade instead of push animation
  • Thinking background changes instantly without animation
  • Assuming invalid subtype causes crash
4. Identify the error in this Swift code that tries to add a transition effect:
let transition = CATransition()
transition.type = "moveIn"
view.layer.add(transition, forKey: nil)
view.backgroundColor = .blue
medium
A. The transition key should not be nil; use a string key instead
B. The transition type "moveIn" is invalid
C. The transition must be added before changing the background color
D. Using nil for the key causes the transition not to work

Solution

  1. Step 1: Check the add method parameters

    The add method requires a non-nil string key to identify the animation. Passing nil disables the animation.
  2. Step 2: Confirm transition type validity

    "moveIn" is a valid transition type, so no error there.
  3. Final Answer:

    The transition key should not be nil; use a string key instead -> Option A
  4. Quick Check:

    Animation key must be non-nil string [OK]
Hint: Always provide a string key when adding animations [OK]
Common Mistakes:
  • Passing nil key disables animation silently
  • Confusing transition type validity
  • Thinking order of adding transition matters
5. You want to create a custom transition that slides a new view controller from the bottom with a duration of 0.5 seconds. Which code snippet correctly sets up this transition effect before presenting the new view controller?
hard
A. let transition = CATransition() transition.duration = 0.5 transition.type = "push" transition.subtype = "fromBottom" view.window?.layer.add(transition, forKey: "transition") present(newVC, animated: false)
B. let transition = CATransition() transition.duration = 0.5 transition.type = "fade" transition.subtype = "fromBottom" view.layer.add(transition, forKey: nil) present(newVC, animated: true)
C. let transition = CATransition() transition.duration = 0.5 transition.type = "moveIn" transition.subtype = "fromTop" view.window?.layer.add(transition, forKey: "slide") present(newVC, animated: true)
D. let transition = CATransition() transition.duration = 0.5 transition.type = "push" transition.subtype = "fromBottom" view.layer.add(transition, forKey: "transition") present(newVC, animated: true)

Solution

  1. Step 1: Choose correct transition type and subtype

    To slide from bottom, use "push" type with "fromBottom" subtype.
  2. Step 2: Add transition to window layer and disable default animation

    Adding to view.window?.layer ensures whole screen transition. Presenting with animated: false disables default animation so custom one shows.
  3. Final Answer:

    Use "push" fromBottom on window layer and present with animated false -> Option A
  4. Quick Check:

    Custom slide from bottom = "push" + "fromBottom" + animated false [OK]
Hint: Add transition to window layer and present with animated false [OK]
Common Mistakes:
  • Adding transition to view.layer instead of window.layer
  • Using animated: true which overrides custom animation
  • Using wrong transition type or subtype
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard