您好,我正在try 实现一个拖拽手势的下拉视图,以关闭它.当我向上执行拖动手势时,视图移动了一会儿,然后冻结,整个模拟器冻结,然后Xcode崩溃.例如,当我在刷卡视图上try 这个完全相同的代码时,它起作用了.为什么它在这种情况下不起作用?

import SwiftUI

struct MessagesHomeView: View {
    @State private var offset = CGSize.zero
    
    @State var showNotifications: Bool = false
        
    var body: some View {
        VStack {
            Button {
                withAnimation(.easeInOut){
                    showNotifications.toggle()
                }
            } label: {
                Image(systemName: "bell").font(.title)
            }
        }
        .overlay {
            VStack {
                if showNotifications {
                    ZStack {
                        Color(UIColor.darkGray)
                        VStack(alignment: .leading){
                            Text("Notifications").font(.title3).foregroundColor(.white)
                            Spacer()
                        }.padding(10).padding(.top, 70)
                        VStack {
                            Spacer()
                            RoundedRectangle(cornerRadius: 10).frame(width: 90, height: 9).foregroundColor(.white)
                                .gesture (
                                    DragGesture()
                                        .onChanged { gesture in
                                            if gesture.translation.height < 0 {
                                                offset = gesture.translation
                                            }
                                        }.onEnded { _ in
                                            withAnimation{
                                                modifyState()
                                            }
                                        }

                                )
                        }.padding(.bottom, 4)
                    }
                    .frame(height: 500)
                    .transition(AnyTransition.move(edge: .top).combined(with: .opacity))
                    .cornerRadius(10, corners: [.bottomLeft, .bottomRight])
                    .offset(y: offset.height)
                    Spacer()
                }
            }.ignoresSafeArea()
        }
    }
    func modifyState(){
        switch offset.height {
        case -600...(-100):
            withAnimation(.easeInOut){
                showNotifications = false
            }
            offset = .zero
        default:
            withAnimation(.easeInOut){
                offset = .zero
            }
        }
    }
}

推荐答案

DragGesture应用于视图的一部分(RoundedRectangle),但偏移量应用于具有该部分(ZStack)的整个容器.这不管用.

try 在.offset修改器之后将拖动手势应用于ZStack:

ZStack {
    Color(UIColor.darkGray)
    VStack(alignment: .leading){
        Text("Notifications").font(.title3).foregroundColor(.white)
        Spacer()
    }.padding(10).padding(.top, 70)
    VStack {
        Spacer()
        RoundedRectangle(cornerRadius: 10).frame(width: 90, height: 9).foregroundColor(.white)
    }.padding(.bottom, 4)
}
.frame(height: 500)
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
.cornerRadius(10, corners: [.bottomLeft, .bottomRight])
.offset(y: offset.height)
.gesture (
    DragGesture()
        .onChanged { gesture in
            if gesture.translation.height < 0 {
                offset = gesture.translation
            }
        }.onEnded { _ in
            withAnimation{
                modifyState()
            }
        }
)
Spacer()

Ios相关问答推荐

带外部笔划的圆形进度视图

SwiftUI.从自定义视图修改器访问自定义视图子视图

不使用iOS 17修改器修改SWIFT用户界面视图

iOS 16.4,SwiftUI 底部工作表有时会忽略presentationDetents

当虚拟对象附加到其网格时,如何刷新创建 ARView 的 SwiftUI UIViewRepresentable?

AVAudioRecord 没有音频

如何将 Info.plist 文件添加到 Xcode for admob,错误 添加后会产生多个命令

ITMS-90725 无用的错误信息,因为它自相矛盾,苹果上传

使用 SceneKit 从 CapturedRoom.walls 重新创建 RoomPlan

Flutter iOS 构建失败并在 ios/Runner/AppDelegate.swift 中出现多个错误

卡在 Apple 的 SwiftUI 教程第 5 章(更新应用数据)

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

通过按钮打开 AppStore

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

如何获取 UICollectionViewCell 的矩形?

单击 GoogleSignIn 按钮时应用程序崩溃

使用 swift 将本地 html 加载到 UIWebView

NSURL 到带有 XCTest 的测试包中的文件路径

如何为 iOS 13 的 SF Symbols 设置权重?

iOS13状态栏背景 colored颜色 与大文本模式下的导航栏不同