CodexBloom - Programming Q&A Platform

Issue with SwiftUI Animation on State Change not Triggering on macOS 14.0

👀 Views: 95 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-31
swiftui animation macos Swift

I'm sure I'm missing something obvious here, but I've looked through the documentation and I'm still confused about I'm working on a SwiftUI application for macOS 14.0, and I have an animation that should trigger when a state variable changes, but it doesn't seem to be working as expected... I'm trying to animate the opacity of a rectangle when a button is pressed to toggle its visibility. Here's the relevant part of my code: ```swift import SwiftUI struct ContentView: View { @State private var isVisible: Bool = false var body: some View { VStack { Rectangle() .fill(Color.blue) .frame(width: 100, height: 100) .opacity(isVisible ? 1 : 0) .animation(.easeInOut(duration: 0.5), value: isVisible) Button("Toggle Visibility") { isVisible.toggle() } } .padding() } } ``` When I run this code, clicking the button toggles the `isVisible` state, but the rectangle's opacity does not animate as expected. Instead, it abruptly changes from fully visible to fully hidden (or vice versa) without any smooth transition. I have tried using both the `.animation()` modifier directly on the rectangle and also encapsulating it within a `withAnimation` block, but neither approach worked. I also checked to ensure there are no conflicting animations applied elsewhere in my code, but everything seems straightforward. Is there a known issue with animations in SwiftUI on macOS 14.0, or am I missing something in my implementation? Any insights would be appreciated! This is part of a larger web app I'm building. Is there a better approach?