SwiftUI - NavigationView 中的意外动画
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 SwiftUI:NavigationView 中的显式动画损坏。
编辑后的解决方案:
解决方案:
Refer to SwiftUI: Broken explicit animations in NavigationView.
Editted solution:
Solution: