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] | | | | | -------------------------
------------------------- | | | | | [Change Color] | | | | | -------------------------
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 }
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.
------------------------- | | | | | [Change Color] | | | | | -------------------------
CATransition in iOS Swift development?CATransition in Swift?let transition = CATransition() transition.type = "push" transition.subtype = "fromRight" view.layer.add(transition, forKey: "transition") view.backgroundColor = .red
let transition = CATransition() transition.type = "moveIn" view.layer.add(transition, forKey: nil) view.backgroundColor = .blue
Pass the test and this pattern moves unknown