Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To fix a section of a SwiftUI view in place while being animated within an NSWindow, you can wrap the fixed section within a separate SwiftUI view and use the .fixedSize() modifier to keep it fixed in place.

Here's an example of how you can achieve this:

import SwiftUI

struct ContentView: View {
    @State private var isAnimating = false

    var body: some View {
        VStack {
            // Separated fixed view
            FixedView()
                .fixedSize()

            // Animating view
            Circle()
                .fill(Color.blue)
                .frame(width: 100, height: 100)
                .offset(x: isAnimating ? 100 : 0, y: 0)
                .animation(.easeInOut(duration: 1))

            Spacer()
        }
        .padding()
    }
}

struct FixedView: View {
    var body: some View {
        Text("Fixed View")
            .font(.largeTitle)
            .fontWeight(.bold)
            .padding()
            .background(Color.gray)
    }
}

In this example, the FixedView is wrapped within a separate view and the .fixedSize() modifier is applied to it to keep it fixed in place. The animating view, Circle, is then placed below the fixed view and animated using the .animation() modifier.

When you run the code, the FixedView will remain fixed in place while the Circle animates horizontally across the screen.