您好,我正在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
}
}
}
}