Skip to content

Commit 0225a8d

Browse files
make gesture simultaneous and do not select an item if the gesture ends outside of a view
1 parent b766f90 commit 0225a8d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Sources/ComponentsKit/RadioGroup/SwiftUI/SURadioGroup.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public struct SURadioGroup<ID: Hashable>: View {
1010
/// A Binding value to control the selected identifier.
1111
@Binding public var selectedId: ID?
1212

13+
@State private var viewSizes: [ID: CGSize] = [:]
1314
@Environment(\.colorScheme) private var colorScheme
1415
@State private var tappingId: ID?
1516

@@ -57,14 +58,29 @@ public struct SURadioGroup<ID: Hashable>: View {
5758
self.model.textColor(for: item).color(for: self.colorScheme)
5859
)
5960
}
60-
.gesture(
61+
.background(
62+
GeometryReader { proxy in
63+
Color.clear
64+
.onAppear {
65+
self.viewSizes[item.id] = proxy.size
66+
}
67+
.onChange(of: proxy.size) { value in
68+
self.viewSizes[item.id] = value
69+
}
70+
}
71+
)
72+
.simultaneousGesture(
6173
DragGesture(minimumDistance: 0)
6274
.onChanged { _ in
6375
self.tappingId = item.id
6476
}
65-
.onEnded { _ in
77+
.onEnded { gesture in
6678
self.tappingId = nil
67-
self.selectedId = item.id
79+
80+
if let size = self.viewSizes[item.id],
81+
CGRect(origin: .zero, size: size).contains(gesture.location) {
82+
self.selectedId = item.id
83+
}
6884
}
6985
)
7086
.disabled(!item.isEnabled || !self.model.isEnabled)

Sources/ComponentsKit/RadioGroup/UIKit/UKRadioGroup.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import AutoLayout
22
import UIKit
33

44
/// A UIKit component that displays a group of radio buttons, allowing users to select one option from multiple choices.
5-
open class UKRadioGroup<ID: Hashable>: UIView, UKComponent {
5+
open class UKRadioGroup<ID: Hashable>: UIView, UKComponent, UIGestureRecognizerDelegate {
66
// MARK: Properties
77

88
/// A closure that is triggered when a selected segment changes.
@@ -81,6 +81,7 @@ open class UKRadioGroup<ID: Hashable>: UIView, UKComponent {
8181
action: #selector(self.handleContainerLongPress(_:))
8282
)
8383
longPressGesture.minimumPressDuration = 0
84+
longPressGesture.delegate = self
8485
radioGroupItemView.addGestureRecognizer(longPressGesture)
8586

8687
self.items[item.id] = radioGroupItemView
@@ -154,14 +155,26 @@ open class UKRadioGroup<ID: Hashable>: UIView, UKComponent {
154155
case .ended:
155156
self.tappingId = nil
156157
self.animateRadioView(for: tappedId, scale: 1.0)
157-
self.selectedId = tappedId
158+
159+
if tappedView.bounds.contains(sender.location(in: tappedView)) {
160+
self.selectedId = tappedId
161+
}
158162
case .cancelled, .failed:
159163
self.tappingId = nil
160164
self.animateRadioView(for: tappedId, scale: 1.0)
161165
default:
162166
break
163167
}
164168
}
169+
170+
// MARK: UKRadioGroup + UIGestureRecognizerDelegate
171+
172+
public func gestureRecognizer(
173+
_ gestureRecognizer: UIGestureRecognizer,
174+
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
175+
) -> Bool {
176+
return true
177+
}
165178
}
166179

167180
// MARK: - Style Helpers

0 commit comments

Comments
 (0)