Ask Your Question
4

How can a section of a SwiftUI view be fixed in place while being animated within an NSWindow?

asked 2023-05-22 03:03:45 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-22 03:07:01 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-22 03:03:45 +0000

Seen: 10 times

Last updated: May 22 '23