Author: saqibkhan

  • SwiftUI Animations MatchedGeometryEffect

    MatchedGeometryEffect

    Animate view identity changes across layouts using .matchedGeometryEffect.


    Syntax

    Matched IDs: Apply .matchedGeometryEffect(id: "id", in: namespace) to two views; SwiftUI animates layout/size/position between them.


    Dot Expands to Large Circle

    Two circles share the same matched geometry ID.

    Toggling state animates size and position between the small and large versions.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct MatchedDemo: View {
      @Namespace private var ns
      @State private var showDetail = false
      var body: some View {
    
    VStack(spacing: 16) {
      if showDetail {
        Circle().matchedGeometryEffect(id: "dot", in: ns).frame(width: 120, height: 120)
      } else {
        Circle().matchedGeometryEffect(id: "dot", in: ns).frame(width: 40, height: 40)
      }
      Button("Toggle") { withAnimation(.spring()) { showDetail.toggle() } }
    }
    .padding()
    } }

    In the example above, the two circles share the same matched geometry ID.

    Toggling state animates size and position between the small and large versions.



    Card Expands to Detail (Shared ID)

    A small card animates into a larger detail view using the same matched geometry ID.

    The transition preserves visual continuity.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct CardsMatchedDemo: View {
      @Namespace private var ns
      @State private var expanded = false
      var body: some View {
    
    VStack(spacing: 16) {
      if expanded {
        RoundedRectangle(cornerRadius: 16)
          .fill(.blue.opacity(0.2))
          .matchedGeometryEffect(id: "card", in: ns)
          .frame(height: 160)
          .overlay(Text("Detail").font(.headline))
      } else {
        RoundedRectangle(cornerRadius: 12)
          .fill(.blue.opacity(0.2))
          .matchedGeometryEffect(id: "card", in: ns)
          .frame(height: 60)
          .overlay(Text("Card").font(.subheadline))
      }
      Button(expanded ? "Close" : "Open") {
        withAnimation(.spring()) { expanded.toggle() }
      }
    }
    .padding()
    } }

    In the example above, the small card animates into a larger detail view using the same matched geometry ID.

  • SwiftUI Animations Transitions

    Transitions

    Animate views as they insert/remove using .transition and withAnimation.


    Syntax

    Transition: .transition(.opacity) on the inserted/removed view.

    Wrap state change in withAnimation to animate the transition.


    Opacity + Scale Transition

    Tap the button to insert/remove a view. The text uses a combined .opacity and .scale transition.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct TransitionDemo: View {
      @State private var show = false
      var body: some View {
    
    VStack(spacing: 12) {
      Button(show ? "Hide" : "Show") {
        withAnimation(.easeInOut) { show.toggle() }
      }
      if show {
        Text("Hello")
          .padding(12)
          .background(.blue.opacity(0.1))
          .cornerRadius(8)
          .transition(.opacity.combined(with: .scale))
      }
    }
    .padding()
    } }

    In the example above, the text uses a combined .opacity and .scale transition.



    Asymmetric Transitions

    Use .asymmetric(insertion:removal:) to customize how the view appears vs disappears.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct AsymmetricTransitionDemo: View {
      @State private var show = false
      var body: some View {
    
    VStack(spacing: 12) {
      Button(show ? "Hide" : "Show") {
        withAnimation(.easeInOut) { show.toggle() }
      }
      if show {
        Text("Panel")
          .padding(12)
          .frame(maxWidth: .infinity)
          .background(.green.opacity(0.15))
          .cornerRadius(8)
          .transition(.asymmetric(insertion: .move(edge: .bottom).combined(with: .opacity),
                                  removal: .move(edge: .top).combined(with: .opacity)))
      }
    }
    .padding()
    } }
  • SwiftUI Animations Explicit

    Explicit

    Wrap state changes in withAnimation to control when and how to animate.


    Syntax

    withAnimation: withAnimation(.easeInOut) { state.toggle() }. All view modifier changes inside the block animate together.


    Rotate on Tap

    Tapping the button triggers withAnimation to rotate the icon by 180 degrees.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ExplicitAnimDemo: View {
      @State private var angle = 0.0
      var body: some View {
    
    VStack(spacing: 12) {
      Image(systemName: "arrow.2.circlepath").rotationEffect(.degrees(angle))
      Button("Rotate") { withAnimation(.easeInOut) { angle += 180 } }
    }
    } }

    In the example above, the arrow icon rotates 180 degrees when the angle state changes.



    Animate Multiple Properties

    Use withAnimation to animate rotation, scale, color, and opacity in a single state change.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ExplicitMultiDemo: View {
      @State private var on = false
      var body: some View {
    
    VStack(spacing: 16) {
      Image(systemName: "star.fill")
        .font(.system(size: 48))
        .foregroundStyle(on ? .yellow : .gray)
        .scaleEffect(on ? 1.3 : 1.0)
        .rotationEffect(.degrees(on ? 180 : 0)) 
        .opacity(on ? 1 : 0.6)
      Button(on ? "Reset" : "Animate") {
        withAnimation(.easeInOut(duration: 0.6)) { on.toggle() }
      }
    }
    } }
  • SwiftUI Animations Implicit

    Implicit

    Attach an animation to a view so changes to a bound value animate automatically.


    Syntax

    Attach animation: .animation(.easeInOut, value: state).

    Any modifier that depends on state will animate when state changes.


    Tap to Grow (Implicit)

    Attaching .animation(..., value: on) to the circle animates its size and color whenever on toggles.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ImplicitAnimDemo: View {
      @State private var on = false
      var body: some View {
    
    VStack(spacing: 12) {
      Circle()
        .fill(on ? .green : .gray)
        .frame(width: on ? 120 : 60, height: on ? 120 : 60)
        .animation(.spring(), value: on)
      Button(on ? "Reset" : "Grow") { on.toggle() }
    }
    .padding()
    } }

    In the example above, the circle’s size and color animate when the on state changes.



    Implicitly Animate Multiple Modifiers

    Attach a single .animation and change several properties; scale, opacity, and color animate when the state toggles.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ImplicitMultiDemo: View {
      @State private var on = false
      var body: some View {
    
    VStack(spacing: 16) {
      Image(systemName: "star.fill")
        .font(.system(size: 48))
        .foregroundStyle(on ? .yellow : .gray)
        .scaleEffect(on ? 1.25 : 1.0)
        .opacity(on ? 1 : 0.6)
        .animation(.easeInOut(duration: 0.6), value: on)
      Button(on ? "Reset" : "Animate") { on.toggle() }
    }
    .padding()
    } }
  • SwiftUI Animations & Transitions

    SwiftUI Animations & Transitions

    Animate view changes with withAnimation and apply .transition for insert/remove effects.


    Implicit Animations

    Attach an animation to a state-driven view modifier so changes animate automatically.

    Syntax:

    • .animation(.easeInOut, value: state)
    • .animation(.spring(), value: state)

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ImplicitDemo: View {
      @State private var on = false
      var body: some View {
    
    Circle()
      .fill(on ? .green : .gray)
      .frame(width: on ? 120 : 60, height: on ? 120 : 60)
      .animation(.spring(), value: on)
      .onTapGesture { on.toggle() }
    } }

    This example animates color and size whenever the boolean state changes, using a spring animation.


    Explicit Animations

    Wrap state changes in withAnimation to control when and how to animate.

    Syntax: withAnimation(.easeInOut) { state.toggle() }

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ExplicitDemo: View {
      @State private var angle = 0.0
      var body: some View {
    
    VStack(spacing: 12) {
      Image(systemName: "arrow.2.circlepath")
        .rotationEffect(.degrees(angle))
      Button("Rotate") {
        withAnimation(.easeInOut) { angle += 180 }
      }
    }
    } }

    This example explicitly animates rotation when the button is pressed.



    Transitions

    Animate views as they insert/remove with .transition and .animation.

    Syntax:

    • .transition(.slide)
    • .transition(.opacity.combined(with: .scale))

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct TransitionDemo: View {
      @State private var show = false
      var body: some View {
    
    VStack(spacing: 12) {
      Button(show ? "Hide" : "Show") {
        withAnimation(.easeInOut) { show.toggle() }
      }
      if show {
        Text("Hello")
          .padding(12)
          .background(.blue.opacity(0.1))
          .cornerRadius(8)
          .transition(.opacity.combined(with: .scale))
      }
    }
    } }
  • SwiftUI Lists & Forms List Styles

    SwiftUI Lists & Forms: List Styles

    Customize list appearance with styles like .insetGrouped.plain, and .sidebar.


    Example: Inset Grouped

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ListStylesInsetDemo: View {
      var body: some View {
    
    List { Text("A"); Text("B"); Text("C") }
      .listStyle(.insetGrouped)
    } }


    Example: Plain with hidden separators

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ListStylesPlainDemo: View {
      var body: some View {
    
    List { Text("A"); Text("B"); Text("C") }
      .listStyle(.plain)
      .listRowSeparator(.hidden)
    } }
  • SwiftUI Lists & Forms Edit Mode

    Edit Mode

    Enable reordering and deletion in lists by toggling EditMode.


    Example: Built-in EditButton

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct EditButtonDemo: View {
      @State private var items = ["A","B","C"]
      var body: some View {
    
    NavigationStack {
      List {
        ForEach(items, id: \.self) { Text($0) }
          .onDelete { items.remove(atOffsets: $0) }
          .onMove { items.move(fromOffsets: $0, toOffset: $1) }
      }
      .navigationTitle("Edit Mode")
      .toolbar { EditButton() }
    }
    } }


    Example: Custom toggle

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct EditCustomDemo: View {
      @State private var items = ["A","B","C"]
      @State private var editMode: EditMode = .inactive
      var body: some View {
    
    VStack {
      List {
        ForEach(items, id: \.self) { Text($0) }
          .onDelete { items.remove(atOffsets: $0) }
          .onMove { items.move(fromOffsets: $0, toOffset: $1) }
      }
      .environment(\.editMode, $editMode)
      Button(editMode.isEditing ? "Done" : "Edit") { editMode.toggle() }
    }
    } } private extension EditMode { mutating func toggle() { self = (isEditing ? .inactive : .active) } }
  • SwiftUI Lists & Forms Swipe Actions

    Swipe Actions

    Add contextual actions with .swipeActions on list rows.


    Example: Trailing delete

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SwipeTrailingDemo: View {
      @State private var items = ["A","B","C"]
      var body: some View {
    
    List {
      ForEach(items, id: \.self) { item in
        Text(item)
          .swipeActions(edge: .trailing) {
            Button(role: .destructive) { items.removeAll { $0 == item } } label: { Label("Delete", systemImage: "trash") }
          }
      }
    }
    } }


    Example: Leading actions

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SwipeLeadingDemo: View {
      @State private var items = ["A","B","C"]
      @State private var favorites: Set<String> = []
      var body: some View {
    
    List {
      ForEach(items, id: \.self) { item in
        HStack { Text(item); if favorites.contains(item) { Image(systemName: "star.fill").foregroundStyle(.yellow) } }
          .swipeActions(edge: .leading) {
            Button { favorites.insert(item) } label: { Label("Favorite", systemImage: "star") }
            Button { /* share */ } label: { Label("Share", systemImage: "square.and.arrow.up") }
          }
      }
    }
    } }
  • SwiftUI Lists & Forms Inputs

    SwiftUI Lists & Forms Inputs

    Collect user input with TextFieldToggle, and Picker.


    Collect input with TextField, Toggle, and Picker

    Use form controls bound to @State to capture text, booleans, and choices.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct InputsDemo: View {
      @State private var name = ""
      @State private var enabled = true
      @State private var choice = 1
      var body: some View {
    
    Form {
      TextField("Name", text: $name)
      Toggle("Enabled", isOn: $enabled)
      Picker("Choice", selection: $choice) {
        Text("One").tag(1)
        Text("Two").tag(2)
      }
    }
    } }

    The example above shows a form with text, toggle, and picker controls bound to @State.



    Segmented Picker & Stepper

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct InputsAdvancedDemo: View {
      @State private var selection = 0
      @State private var count = 1
      var body: some View {
    
    Form {
      Picker("Options", selection: $selection) {
        Text("A").tag(0); Text("B").tag(1); Text("C").tag(2)
      }
      .pickerStyle(.segmented)
      Stepper("Count: \(count)", value: $count, in: 1...5)
    }
    } }
  • SwiftUI Lists & Forms Form

    SwiftUI Lists & Forms: Form

    Group input controls with Form to build settings and data-entry screens.


    Example: Basic Form

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct FormBasicDemo: View {
      @State private var name = ""
      @State private var notifications = true
      var body: some View {
    
    Form {
      Section(header: Text("Profile")) { TextField("Name", text: $name) }
      Section(header: Text("Preferences")) { Toggle("Notifications", isOn: $notifications) }
    }
    } }


    Example: Validation & Submit

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct FormAdvancedDemo: View {
      @State private var email = ""
      @State private var accepted = false
      var valid: Bool { email.contains("@") && accepted }
      var body: some View {
    
    Form {
      Section(header: Text("Account")) {
        TextField("Email", text: $email).keyboardType(.emailAddress)
        Toggle("Accept Terms", isOn: $accepted)
      }
      Section { Button("Submit") { } .disabled(!valid) }
    }
    } }