Skip to content

ConditionalEffect doesn't stop immediately when condition becomes false #65

Open
@davidpasztor

Description

@davidpasztor

I want to mimic the edit mode of the iOS Home Screen, where cells wiggle while in edit mode and stop wiggling as soon as the user exits edit mode.

I've tried using .conditionalEffect(.repeat(.wiggle, every: .seconds(0.1)), condition: shouldWiggle), but when the condition becomes false, the effect doesn't stop immediately.

Here's a minimum, reproducible example showcasing the issue:

import Pow
import SwiftUI

struct WiggleTester: View {
  @State private var shouldWiggle: Bool = false
  let cellModels: [String]

  init() {
    self.init(
      cellModels: (1...10).map(\.description)
    )
  }

  init(cellModels: [String]) {
    self.cellModels = cellModels
  }

  var body: some View {
    VStack {
      grid
        .frame(maxWidth: .infinity, maxHeight: .infinity)
      Button(
        action: { shouldWiggle.toggle() },
        label: { Text("Turn Wiggle \(shouldWiggle ? "OFF" : "ON")") }
      )
    }
  }

  @ViewBuilder
  private var grid: some View {
    let columns = [
      // Using adaptive columns with a min size to allow the system to fit the appropriate number
      // of columns on the screen.
      GridItem(.adaptive(minimum: 85), spacing: 8)
    ]
    LazyVGrid(columns: columns) {
      ForEach(cellModels, id: \.self) { cellModel in
        cell(model: cellModel)
          .conditionalEffect(
            .repeat(.wiggle, every: .seconds(0.1)),
            condition: shouldWiggle
          )
      }
    }
  }

  private func cell(model: String) -> some View {
    Button(
      action: {
        print("Button tapped \(model)")
      }, label: {
        VStack {
          Text(model)
            .padding()
            .foregroundStyle(Color.primary)
            .background { Color.secondary }
            .clipShape(Circle())
          Text(model)
        }
      }
    )
  }
}

Screen recording of the problem:
wiggle issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions