Skip to content

Commit 22f3f12

Browse files
Use onTouchDown instead of onTap for faster View updates
1 parent 3e08cde commit 22f3f12

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Sources/Focuser/TextEditorIntrospect.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public struct FocusModifierTextEditor<Value: FocusStateCompliant & Hashable>: Vi
1919
tv.becomeFirstResponder()
2020
}
2121
}
22-
.simultaneousGesture(TapGesture().onEnded {
23-
focusedField = equals
24-
})
22+
.onTouchDownGesture {
23+
focusedField = equals
24+
}
2525
}
2626
}

Sources/Focuser/TextFieldIntrospect.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public struct FocusModifier<Value: FocusStateCompliant & Hashable>: ViewModifier
100100
}
101101
}
102102
}
103-
.simultaneousGesture(TapGesture().onEnded {
103+
.onTouchDownGesture {
104104
focusedField = equals
105-
})
105+
}
106106
}
107107
}

Sources/Focuser/View+Extension.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tarek Sabry on 09/12/2022.
6+
//
7+
8+
import SwiftUI
9+
10+
extension View {
11+
func onTouchDownGesture(callback: @escaping () -> Void) -> some View {
12+
modifier(OnTouchDownGestureModifier(callback: callback))
13+
}
14+
}
15+
16+
private struct OnTouchDownGestureModifier: ViewModifier {
17+
@State private var tapped = false
18+
let callback: () -> Void
19+
20+
func body(content: Content) -> some View {
21+
content
22+
.simultaneousGesture(DragGesture(minimumDistance: 0)
23+
.onChanged { _ in
24+
if !tapped {
25+
tapped = true
26+
callback()
27+
}
28+
}
29+
.onEnded { _ in
30+
tapped = false
31+
})
32+
}
33+
}

0 commit comments

Comments
 (0)