Skip to content

Commit 0440196

Browse files
committed
ADD: Button Haptic Feature
1 parent 436e6db commit 0440196

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Sources/OpenTDS/Component/TabView/TabView+Button.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public struct TossTabViewButton: View {
1515
let image: Image
1616
let action: () -> ()
1717
var selected: Bool
18+
let haptic: UIImpactFeedbackGenerator.FeedbackStyle?
1819
@State var buttonRect: CGRect = CGRect.zero
1920
@State var animationState: Int = 0
2021
@State var touchdownState: Bool = false
@@ -30,12 +31,14 @@ public struct TossTabViewButton: View {
3031
public init(_ label: String,
3132
_ image: Image,
3233
_ selected: Bool,
34+
haptic: UIImpactFeedbackGenerator.FeedbackStyle? = nil,
3335
action: @escaping () -> ())
3436
{
3537
self.label = label
3638
self.image = image
3739
self.selected = selected
3840
self.action = action
41+
self.haptic = haptic
3942
}
4043

4144
public var head: some View {
@@ -84,7 +87,10 @@ public struct TossTabViewButton: View {
8487
}
8588
.onEnded { _ in
8689
if touchdownState {
87-
UIImpactFeedbackGenerator(style: .light).impactOccurred()
90+
if let haptic = haptic {
91+
UIImpactFeedbackGenerator(style: haptic)
92+
.impactOccurred()
93+
}
8894
action()
8995
}
9096
for i in 2..<5 {

Sources/OpenTDS/Component/TabView/TabView.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ public struct TossTabView: View {
2020
let content: [any TossTabItemViewProtocol]
2121

2222
public init<C0: TossTabItemViewProtocol,
23-
C1: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1)>)
23+
C1: TossTabItemViewProtocol>(haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
24+
@ViewBuilder content: @escaping () -> TupleView<(C0, C1)>)
2425
{
2526
let cv = content().value
2627
self.content = [cv.0, cv.1]
2728
}
2829

2930
public init<C0: TossTabItemViewProtocol,
3031
C1: TossTabItemViewProtocol,
31-
C2: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2)>)
32+
C2: TossTabItemViewProtocol>(haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
33+
@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2)>)
3234
{
3335
let cv = content().value
3436
self.content = [cv.0, cv.1, cv.2]
@@ -37,7 +39,8 @@ public struct TossTabView: View {
3739
public init<C0: TossTabItemViewProtocol,
3840
C1: TossTabItemViewProtocol,
3941
C2: TossTabItemViewProtocol,
40-
C3: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3)>)
42+
C3: TossTabItemViewProtocol>(haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
43+
@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3)>)
4144
{
4245
let cv = content().value
4346
self.content = [cv.0, cv.1, cv.2, cv.3]
@@ -47,7 +50,8 @@ public struct TossTabView: View {
4750
C1: TossTabItemViewProtocol,
4851
C2: TossTabItemViewProtocol,
4952
C3: TossTabItemViewProtocol,
50-
C4: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3, C4)>)
53+
C4: TossTabItemViewProtocol>(haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
54+
@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3, C4)>)
5155
{
5256
let cv = content().value
5357
self.content = [cv.0, cv.1, cv.2, cv.3, cv.4]
@@ -77,7 +81,9 @@ public struct TossTabView: View {
7781
HStack {
7882
Spacer()
7983
ForEach(0..<content.count, id: \.self) { idx in
80-
TossTabViewButton(content[idx].title, content[idx].image, idx == selected) {
84+
TossTabViewButton(content[idx].title,
85+
content[idx].image,
86+
idx == selected) {
8187
if selected != idx {
8288
withAnimation(.easeInOut(duration: 0.2)) {
8389
selected = idx

0 commit comments

Comments
 (0)