0
0
iOS Swiftmobile~5 mins

Why animations polish user experience in iOS Swift

Choose your learning style9 modes available
Introduction

Animations make apps feel smooth and alive. They help users understand what is happening on the screen.

When showing a new screen or page to make the change less sudden
When a button is tapped to give feedback that it was pressed
When loading content to show progress and keep users engaged
When moving or changing items to show how things relate or update
When drawing attention to important parts of the app
Syntax
iOS Swift
UIView.animate(withDuration: TimeInterval, animations: @escaping () -> Void)

This is the basic way to create animations in iOS using Swift.

You specify how long the animation lasts and what changes to animate inside the closure.

Examples
This fades out a view by changing its transparency over half a second.
iOS Swift
UIView.animate(withDuration: 0.5) {
  myView.alpha = 0
}
This moves a view 100 points to the right smoothly in one second.
iOS Swift
UIView.animate(withDuration: 1.0) {
  myView.frame.origin.x += 100
}
This makes a button grow slightly to show it was tapped.
iOS Swift
UIView.animate(withDuration: 0.3) {
  myButton.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}
Sample App

This app shows a blue square. When you tap it, the square moves right and changes color to red smoothly over half a second.

iOS Swift
import UIKit

class ViewController: UIViewController {
  let squareView = UIView()

  override func viewDidLoad() {
    super.viewDidLoad()
    squareView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    squareView.backgroundColor = .systemBlue
    view.addSubview(squareView)

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(animateSquare))
    squareView.addGestureRecognizer(tapGesture)
    squareView.isUserInteractionEnabled = true
  }

  @objc func animateSquare() {
    UIView.animate(withDuration: 0.5) {
      self.squareView.backgroundColor = .systemRed
      self.squareView.frame.origin.x += 150
    }
  }
}
OutputSuccess
Important Notes

Animations usually run on the main thread and should be short to keep the app responsive.

Use animations to guide the user's attention and make interactions clear.

Common mistake: animating too many things at once can slow down the app.

Summary

Animations make apps feel smooth and easy to use.

They help users understand changes on screen.

Use UIView.animate to create simple animations in Swift.