dai*_*dai 6 swiftui draggesture
我正在构建一个自定义模态,当我拖动模态时,任何附加动画的子视图都会在我拖动时进行动画处理。我该如何阻止这种情况发生?
我考虑过传递@EnvironmentObject带有isDragging标志的 an ,但它的可扩展性不太好(并且不能很好地与自定义ButtonStyles 配合使用)
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())
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
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)
Run Code Online (Sandbox Code Playgroud)
在现实生活中,容器包含一个按钮,在按下状态下有动画
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 0.9 : 1)
.animation(.spring())
}
}
Run Code Online (Sandbox Code Playgroud)
将animationsDisabled 函数添加到子视图中,该函数实际上会阻止子视图在拖动过程中移动。
它不会做的是当生物最初滑入或消失时停止动画。
有没有办法知道视图何时基本上没有移动/转换?
理论上,在这种情况下 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) {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3584 次 |
| 最近记录: |