When I swipe left, the view freezes but the menu component still works when swiping. Also, when it’s frozen, if I just click anywhere in the frozen area, it toggles the menu, and then when I close the menu it’s unfrozen and works again. What might be the problem?
.frame(width: getRect().width)
.overlay(
Rectangle()
.fill(
Color.primary
.opacity(Double((offset / sideBarWidth / 5)))
)
.ignoresSafeArea(.container, edges: .vertical)
.onTapGesture {
withAnimation {
menuIsShowing.toggle()
}
}
)
}
.frame(width: getRect().width + sideBarWidth)
.offset(x: -sideBarWidth / 2)
.offset(x: offset > 0 ? offset : 0)
.gesture(
DragGesture()
.updating($gestureOffset, body: { value, out, _ in
out = value.translation.width
})
.onEnded(onEnd(value:))
)
.navigationDestination(isPresented: $showChat, destination: {
if let user = selectedUser {
ChatView(user: user)
}
})
.onChange(of: selectedUser, perform: { newValue in
showChat = newValue != nil
})
.animation(.easeInOut, value: offset == 0)
.onChange(of: menuIsShowing) { newValue in
if menuIsShowing && offset == 0 {
offset = sideBarWidth
lastStoredOffset = offset
}
if !menuIsShowing && offset == sideBarWidth {
offset = 0
lastStoredOffset = 0
}
}
.onChange(of: gestureOffset) { newValue in
onChange()
}
}
}
func onChange() {
let sideBarWidth = getRect().width - 90
offset = (gestureOffset != 0) ? (gestureOffset + lastStoredOffset < sideBarWidth ? gestureOffset + lastStoredOffset: offset) : offset
}
func onEnd(value: DragGesture.Value) {
let sideBarWidth = getRect().width - 90
let translation = value.translation.width
withAnimation {
if translation > 0 {
if translation > (sideBarWidth / 2) {
offset = sideBarWidth
menuIsShowing = true
}
else {
if offset == sideBarWidth {
return
}
offset = 0
menuIsShowing = false
}
}
else {
if -translation > (sideBarWidth / 2) {
offset = 0
menuIsShowing = false
}
else {
if offset == 0 || !menuIsShowing {
return
}
offset = sideBarWidth
menuIsShowing = true
}
}
}
lastStoredOffset = offset
I tried the code below, but it stops the wanted swipe drag gesture.
if value.translation.width > 0
out = value.translation.width
}
New contributor
Michael Amato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.