SwiftUI - NavigationView 中的意外动画

发布于 2025-01-09 11:09:45 字数 1387 浏览 4 评论 0原文

我在 VStack 中有 3 个文本视图,我希望中间的一个通过使用scaleEffect 来反弹。然而,还有一些额外的意想不到的上下动画。

我发现一旦我从 NavigationView 中删除视图,它就会表现正常。

是我这边的问题还是苹果这边的问题?以及如何修复它?谢谢。

// TestingApp.swift

import SwiftUI

@main
struct TestingApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                LaunchView()
            }
        }
    }
}
// LaunchView.swift

import SwiftUI

struct LaunchView: View {
    var body: some View {
        NavigationLink("To ContentView", destination: ContentView())
    }
}

struct LaunchView_Previews: PreviewProvider {
    static var previews: some View {
        LaunchView()
    }
}
// ContentView.swift

import SwiftUI

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8
    
    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    withAnimation(.spring().repeatForever(autoreverses: true)) {
                        scaleFactor = 1
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I have 3 Text View in a VStack, and I want the middle one to bounce by using scaleEffect. However, there is some additional unexpected up and down animation.

I found that once I remove the view from NavigationView, it will behave normal.

Is the problem on my side or Apple's side? And how to fix it? Thanks.

// TestingApp.swift

import SwiftUI

@main
struct TestingApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                LaunchView()
            }
        }
    }
}
// LaunchView.swift

import SwiftUI

struct LaunchView: View {
    var body: some View {
        NavigationLink("To ContentView", destination: ContentView())
    }
}

struct LaunchView_Previews: PreviewProvider {
    static var previews: some View {
        LaunchView()
    }
}
// ContentView.swift

import SwiftUI

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8
    
    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    withAnimation(.spring().repeatForever(autoreverses: true)) {
                        scaleFactor = 1
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2025-01-16 11:09:45

请参阅 SwiftUI:NavigationView 中的显式动画损坏

编辑后的解决方案:

// ContentView.swift

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8

    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.milliseconds(200)) { // delay for 0.2 seconds
                            withAnimation(.spring().repeatForever(autoreverses: true)) {
                                scaleFactor = 1
                        }
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}

解决方案:

// ContentView.swift

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8
    
    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    DispatchQueue.main.async {
                        withAnimation(.spring().repeatForever(autoreverses: true)) {
                            scaleFactor = 1
                        }
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}

Refer to SwiftUI: Broken explicit animations in NavigationView.

Editted solution:

// ContentView.swift

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8

    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.milliseconds(200)) { // delay for 0.2 seconds
                            withAnimation(.spring().repeatForever(autoreverses: true)) {
                                scaleFactor = 1
                        }
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}

Solution:

// ContentView.swift

struct ContentView: View {
    @State private var scaleFactor: CGFloat = 0.8
    
    var body: some View {
        VStack {
            Text("Text above")
            Text("Text bouncing")
                .scaleEffect(scaleFactor)
                .onAppear {
                    DispatchQueue.main.async {
                        withAnimation(.spring().repeatForever(autoreverses: true)) {
                            scaleFactor = 1
                        }
                    }
                }
            Text("Text below")
        }
        .font(.largeTitle)
        .padding()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文