Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix for segmented control getting stuck when gesture cancelled during…
Browse files Browse the repository at this point in the history
… scroll
  • Loading branch information
StephenHeaps committed Oct 4, 2023
1 parent 82eedbb commit e3781d6
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct PortfolioSegmentedControl: View {
@Binding var selected: SelectedContent
@State private var viewSize: CGSize = .zero
@State private var location: CGPoint = .zero
@GestureState private var isDragGestureActive: Bool = false

var body: some View {
GeometryReader { geometryProxy in
Expand Down Expand Up @@ -93,6 +94,16 @@ struct PortfolioSegmentedControl: View {
}
.frame(height: 40)
.gesture(dragGesture)
.onChange(of: isDragGestureActive) { isDragGestureActive in
if !isDragGestureActive { // cancellation of gesture, ex while scrolling
var newX = location.x
if newX < viewSize.width / 2 {
select(.assets)
} else {
select(.nfts)
}
}
}
.osAvailabilityModifiers {
if #available(iOS 16, *) {
$0.simultaneousGesture(tapGesture)
Expand All @@ -114,6 +125,9 @@ struct PortfolioSegmentedControl: View {

private var dragGesture: some Gesture {
DragGesture()
.updating($isDragGestureActive) { value, state, transaction in
state = true
}
.onChanged { value in
var newX = value.location.x
if newX < viewSize.width / 4 {
Expand Down

0 comments on commit e3781d6

Please sign in to comment.