ios - 防止 SwiftUI 动画覆盖嵌套的 withAnimation block

标签 ios swiftui swiftui-animation

我有一个带有可拖动组件(“弹性”文本)的屏幕,当释放时会弹回一个非常突出的 Spring 动画。

有些文本是异步传入的(在本例中,使用 Timer),我希望它能够很好地淡入,同时 Springy 标签平滑地向上移动以为它腾出空间。

Screenshot of example code

我在 VStack 中添加了一个动画,当异步加载的文本淡入时,它会按照我的意愿转换。但是,这会破坏“有弹性”文本上的 Spring 动画。

这里有一个动画被注释掉,如果将 1 秒动画切换到该位置,异步加载的文本会淡入,但在动画开始时,“有弹性”的文本会向上跳而不是向上滑动。

我怎样才能让两个动画都按照我的意愿工作?

import SwiftUI
import Combine

class Store: ObservableObject {
    @Published var showSection: Bool = false
    
    private var cancellables: [AnyCancellable] = []
    
    init() {
        
        Timer
            .publish(every: 2, on: RunLoop.main, in: .common)
            .autoconnect()
            .map { _ in true}
            .assign(to: \.showSection,
                    on: self)
            .store(in: &cancellables)
    }
}

struct ContentView: View {
    
    @ObservedObject var store = Store()
    
    @State var draggedState = CGSize.zero
    
    var body: some View {
        VStack {
            Spacer()
            VStack(alignment: .leading) {
                VStack(spacing: 4) {
                    HStack {
                        Text("Springy")
                            .font(.title)
                        Image(systemName: "hand.point.up.left.fill")
                            .imageScale(.large)
                            
                    }
                    .offset(x: draggedState.width, y: draggedState.height)
                    .foregroundColor(.secondary)
                    .gesture(
                        DragGesture().onChanged { value in
                            draggedState = value.translation
                        }
                        .onEnded { value in
                            // How can this animation be fast without the other animation slowing it down?
                            withAnimation(Animation
                                            .interactiveSpring(response: 0.3,
                                                               dampingFraction: 0.1,
                                                               blendDuration: 0)) {
                                draggedState = .zero
                            }
                        }
                    )
                    
                    VStack {
                        if store.showSection {
                            Text("Something that animates in after loading asynchrously.")
                                .font(.body)
                                .padding()
                                .transition(.opacity)
                        }
                    }
                    // When the animation is here, it doesn't cancel out the spring animation, but the Springy text jumps up at the beginning of the animation instead of animating.
//                    .animation(.easeInOut(duration: 1))
                }
                // Animation here has desired effect for loading, but overrides the Springy release animation.
                .animation(.easeInOut(duration: 1))
            }
            Spacer()
        }
    }
}

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

扩展问题,基于 Asperi 的回答。

现在,假设我在这个主 VStack 中有多个元素,我想在它们进入时对其进行动画处理。

  1. 为我要制作动画的每个部分添加一个带有 value 的单独 .animation 修饰符是否是一个好的解决方案?

在下面的扩展示例中,这部分包含:

.animation(.easeInOut(duration: 1),
                       value: store.showSection)
.animation(.easeInOut(duration: 1),
                      value: store.somePossibleText)
.animation(.easeInOut(duration: 1),
                      value: store.moreInformation)
  1. 我在这里使用 3 种方法来确定是否显示某个部分。为了简单起见,我在初始示例中使用了 Bool,但在我的实际场景中,我认为使用 String? 方法而不是设置单独的 Bool 或检查空字符串。在动画方面使用它是否有缺点,或者这样好吗?考虑到 Asperi 的解决方案,它似乎在这个示例中运行良好。

以下是新修改的完整示例:

import SwiftUI
import Combine

class Store: ObservableObject {
    
    @Published var showSection: Bool = false
    @Published var somePossibleText: String = ""
    @Published var moreInformation: String?
    
    private var cancellables: [AnyCancellable] = []
    
    init() {
        
        Timer
            .publish(every: 2, on: RunLoop.main, in: .common)
            .autoconnect()
            .map { _ in true }
            .assign(to: \.showSection,
                    on: self)
            .store(in: &cancellables)
        
        Timer
            .publish(every: 3, on: RunLoop.main, in: .common)
            .autoconnect()
            .map { _ in "Something else loaded later" }
            .assign(to: \.somePossibleText,
                    on: self)
            .store(in: &cancellables)
        
        Timer
            .publish(every: 4, on: RunLoop.main, in: .common)
            .autoconnect()
            .map { _ in "More stuff loaded later" }
            .assign(to: \.moreInformation,
                    on: self)
            .store(in: &cancellables)
    }
}

struct ContentView: View {
    
    @ObservedObject var store = Store()
    
    @State var draggedState = CGSize.zero
    
    var body: some View {
        VStack {
            Spacer()
            VStack(alignment: .leading) {
                VStack(spacing: 4) {
                    HStack {
                        Text("Springy")
                            .font(.title)
                        Image(systemName: "hand.point.up.left.fill")
                            .imageScale(.large)
                    }
                    .offset(x: draggedState.width,
                            y: draggedState.height)
                    .foregroundColor(.secondary)
                    .gesture(
                        DragGesture().onChanged { value in
                            draggedState = value.translation
                        }
                        .onEnded { value in
                            // TODO: how can this animation be fast without the other one slowing it down?
                            withAnimation(Animation
                                            .interactiveSpring(response: 0.3,
                                                               dampingFraction: 0.1,
                                                               blendDuration: 0)) {
                                draggedState = .zero
                            }
                        }
                    )
                    
                    if store.showSection {
                        Text("Something that animates in after loading asynchrously.")
                            .font(.body)
                            .padding()
                            .transition(.opacity)
                    }
                    
                    if store.somePossibleText != "" {
                        Text(store.somePossibleText)
                            .font(.footnote)
                            .padding()
                            .transition(.opacity)
                    }
                    
                    if let moreInformation = store.moreInformation {
                        Text(moreInformation)
                            .font(.footnote)
                            .padding()
                            .transition(.opacity)
                    }
                }
                .animation(.easeInOut(duration: 1),
                           value: store.showSection)
                .animation(.easeInOut(duration: 1),
                           value: store.somePossibleText)
                .animation(.easeInOut(duration: 1),
                           value: store.moreInformation)
            }
            Spacer()
        }
    }
}

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

最佳答案

如果我正确理解您的期望,将最后一个动画绑定(bind)到 showSection 值就足够了,如下所示

}
// Animation here has desired effect for loading, but overrides the Springy release animation.
.animation(.easeInOut(duration: 1), value: store.showSection)

使用 Xcode 12/iOS 14 测试。

关于ios - 防止 SwiftUI 动画覆盖嵌套的 withAnimation block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64518733/

相关文章:

ios - 如何使用分页更改 tableview 自定义单元格中 imageview 的图像。

swift - 在 SwiftUI 中为路径描边绘制动画

swift - 如何使用 SwiftUI 和 CoreData (@FetchRequest) 对动态列表排序进行动画处理

ios - 过渡在 NavigationView 内不起作用

ios - 如何提高 iOS 汉堡菜单的 CSS 性能?

ios - iPhone 屏幕方向在 cordova 3.5.0 中没有改变

swift - 当 AsyncImage SwiftUI 中的图像 URL 发生变化时,如何监听新阶段的成功

ios - SwiftUI 和 UIKit 与旧 API 中的 UIViewController 要求交互

javascript - Phonegap fabebook SDK - FB.logout 不工作