4

我正在构建一个自定义模态,当我拖动模态时,任何附加了动画的子视图都会在我拖动时动画。我该如何阻止这种情况发生?

@EnvironmentObject我考虑过用标志传递 an isDragging,但它的可扩展性不是很好(并且不适用于 custom ButtonStyle

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
            .showModal(isShowing: .constant(true))
    }
}

extension View {
    func showModal(isShowing: Binding<Bool>) -> some View {
        ViewOverlay(isShowing: isShowing, presenting: { self })
    }
}

struct ViewOverlay<Presenting>: View where Presenting: View {
    @Binding var isShowing: Bool
    
    let presenting: () -> Presenting
    
    @State var bottomState: CGFloat = 0
    
    var body: some View {
        ZStack(alignment: .center) {
            presenting().blur(radius: isShowing ? 1 : 0)
            VStack {
                if isShowing {
                    Container()
                        .background(Color.red)
                        .offset(y: bottomState)
                        .gesture(
                            DragGesture()
                                .onChanged { value in
                                    bottomState = value.translation.height
                                }
                                .onEnded { _ in
                                    if bottomState > 50 {
                                        withAnimation {
                                            isShowing = false
                                        }
                                    }
                                    bottomState = 0
                                })
                        .transition(.move(edge: .bottom))
                }
            }
        }
    }
}

struct Container: View {
    var body: some View {
// I want this to not animate when dragging the modal
        Text("CONTAINER")
            .frame(maxWidth: .infinity, maxHeight: 200)
            .animation(.spring())
    }
}


用户界面

更新:

extension View {
    func animationsDisabled(_ disabled: Bool) -> some View {
        transaction { (tx: inout Transaction) in
            tx.animation = tx.animation
            tx.disablesAnimations = disabled
        }
    }
}


Container()
   .animationsDisabled(isDragging || bottomState > 0)

在现实生活中,容器包含一个按钮,在其按下状态下会有动画

struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.9 : 1)
            .animation(.spring())
    }
}

向子视图添加了 animationsDisabled 函数,它实际上阻止了子视图在拖动过程中的移动。

它不会在最初滑入或关闭时停止动画。

有没有办法知道视图何时基本上没有移动/过渡?

4

3 回答 3

3

理论上 SwiftUI 在这种情况下不应该翻译动画,但是我不确定这是否是一个错误——我不会以那种通用的方式在 Container 中使用动画。我使用动画的次数越多,就越倾向于将它们直接连接到特定值。

无论如何......这是可能的解决方法 - 通过在中间注入不同的托管控制器来破坏动画可见性。

使用 Xcode 12 / iOS 14 测试

演示

struct ViewOverlay<Presenting>: View where Presenting: View {
    @Binding var isShowing: Bool
    
    let presenting: () -> Presenting
    
    @State var bottomState: CGFloat = 0
    
    var body: some View {
        ZStack(alignment: .center) {
            presenting().blur(radius: isShowing ? 1 : 0)
            VStack {
                    Color.clear
                if isShowing {
                        HelperView {
                    Container()
                        .background(Color.red)
                        }
                        .offset(y: bottomState)
                        .gesture(
                             DragGesture()
                                  .onChanged { value in
                                        bottomState = value.translation.height
                                  }
                                  .onEnded { _ in
                                        if bottomState > 50 {
                                             withAnimation {
                                                  isShowing = false
                                             }
                                        }
                                        bottomState = 0
                                  })
                        .transition(.move(edge: .bottom))
                }
                    Color.clear
            }
        }
    }
}

struct HelperView<Content: View>: UIViewRepresentable {
    let content: () -> Content
    func makeUIView(context: Context) -> UIView {
        let controller = UIHostingController(rootView: content())
        return controller.view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
    }
}
于 2020-10-08T18:14:54.343 回答
1

所以这是我更新的答案。我认为没有一种很好的方法可以做到这一点,所以现在我正在使用自定义按钮来做到这一点。

import SwiftUI

struct ContentView: View {
    @State var isShowing = false
    var body: some View {
        Text("Hello, world!")
            .padding()
            .onTapGesture(count: 1, perform: {
                withAnimation(.spring()) {
                    self.isShowing.toggle()
                }
            })
            .showModal(isShowing: self.$isShowing)
    }
}

extension View {
    func showModal(isShowing: Binding<Bool>) -> some View {
        ViewOverlay(isShowing: isShowing, presenting: { self })
    }
}

struct ViewOverlay<Presenting>: View where Presenting: View {
    @Binding var isShowing: Bool
    
    let presenting: () -> Presenting
    
    @State var bottomState: CGFloat = 0
    @State var isDragging = false
    var body: some View {
        ZStack(alignment: .center) {
            presenting().blur(radius: isShowing ? 1 : 0)
            VStack {
                if isShowing {
                    Container()
                        .background(Color.red)
                        .offset(y: bottomState)
                        .gesture(
                            DragGesture()
                                .onChanged { value in
                                    isDragging = true
                                    bottomState = value.translation.height
                                    
                                }
                                .onEnded { _ in
                                    isDragging = false
                                    if bottomState > 50 {
                                        withAnimation(.spring()) {
                                            isShowing = false
                                        }
                                    }
                                    bottomState = 0
                                })
                        .transition(.move(edge: .bottom))
                }
            }
        }
    }
}

struct Container: View {
    var body: some View {
        CustomButton(action: {}, label: {
            Text("Pressme")
        })
        .frame(maxWidth: .infinity, maxHeight: 200)
    }
}

struct CustomButton<Label >: View where Label: View {
    @State var isPressed = false
    var action: () -> ()
    var label: () -> Label
    var body: some View {
        label()
            .scaleEffect(self.isPressed ? 0.9 : 1.0)
        .gesture(DragGesture(minimumDistance: 0).onChanged({_ in
            withAnimation(.spring()) {
                self.isPressed = true
            }
        }).onEnded({_ in
            withAnimation(.spring()) {
                self.isPressed = false
                action()
            }
        }))
    }
}

问题是您不能在容器内使用隐式动画,因为它们会在移动时进行动画处理。withAnimation因此,您还需要为按下的按钮显式设置动画,我现在使用自定义按钮和 DragGesture 进行了设置。

这是显式动画和隐式动画之间的区别。

观看此视频,其中详细探讨了该主题:

https://www.youtube.com/watch?v=3krC2c56ceQ&list=PLpGHT1n4-mAtTj9oywMWoBx0dCGd51_yG&index=11

于 2020-10-08T07:57:13.223 回答
1

Container中,声明一个绑定变量,以便您可以将 传递bottomStateContainer视图:

struct Container: View {
    
    @Binding var bottomState: CGFloat

              .
              .
              .
              .
}

不要忘记在您使用它的任何地方传递bottomState给您的视图:Container

Container(bottomState: $bottomState)

现在在您的Container视图中,您只需要声明在更改时您不想要动画bottomState

Text("CONTAINER")
            .frame(maxWidth: .infinity, maxHeight: 200)
            .animation(nil, value: bottomState) // You Need To Add This
            .animation(.spring())

.animation(nil, value: bottomState)中,nil您正在向 SwiftUI 询问no动画,而valueofbottomState正在更改。

此方法使用 Xcode 12 GM、iOS 14.0.1 进行测试。Text您必须按照我放置它们的顺序使用修饰符。这意味着这将起作用:

.animation(nil, value: bottomState)
.animation(.spring())

但这不起作用:

.animation(.spring())
.animation(nil, value: bottomState)

我还确保添加.animation(nil, value: bottomState)只会在更改时禁用动画,并且如果没有bottomState更改,动画.animation(.spring())应该始终有效bottomState

于 2020-10-09T01:16:54.990 回答