I have a weird issue on SwiftUI iOS 18, when using DragGesture and dragging a view from the left side of the screen (In that cases), causes any View and Sub-View with @AppStorage
in it to reinitialize which causes for a junky animation
ZStack {
DestinationTabs()
.presentVideoPlayer()
SideMenuView(show: $show)
.offset(x: -Constants.sideMenuWidth)
.offset(x:max( min(dragging.width + position.width ,Constants.sideMenuWidth),0) )
}
.simultaneousGesture(
DragGesture()
.onChanged { value in
withAnimation(.linear) {
if position.width == 0 && value.translation.width < 0 {
self.dragging = .zero
} else {
if value.startLocation.x <= 30 && self.position.width < Constants.sideMenuWidth{
self.dragging = value.translation
let adjustment = position.width == Constants.sideMenuWidth ? 0 : (value.translation.width / 625)
self.scale = position.width + value.translation.width >= Constants.sideMenuWidth ? 0.9 : max(1 - abs(adjustment), 0.9)
}
}
}
}
.onEnded { value in
withAnimation(.linear) {
if value.translation.width >= 50 && value.startLocation.x <= 30{
self.position.width += (value.translation.width > 0) ? Constants.sideMenuWidth : -Constants.sideMenuWidth
} else if self.position.width != Constants.sideMenuWidth{
self.position.width = 0
self.scale = 1.0
}
self.dragging = .zero
}
})
With the above code, any view within “DestinationTabs” refresh itself multiple time when onChanged
is called.
After few days of debugging, I observed that deleting @AppStorage
vars/properties from the view completely resolves the issue, DragGesture
does not modify any @AppStorage
vars and it’s simply moving the SideMenuView
from left to right.
Any ideas?