Skip to content

Commit 07072f2

Browse files
committed
Normalize gestures API
1 parent 2a34f2c commit 07072f2

8 files changed

+242
-164
lines changed

Classes/Extensions/DeclarativeProtocol+Hover.swift

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ import UIKit
22

33
extension DeclarativeProtocol {
44
@discardableResult
5-
public func onHoverGesture(_ action: @escaping (UIGestureRecognizer.State) -> Void) -> Self {
6-
if #available(iOS 13.0, *) {
7-
declarativeView.addGestureRecognizer(HoverGestureRecognizer().trackState(action))
5+
public func onHoverGesture(_ action: @escaping (Self) -> Void) -> Self {
6+
onHoverGesture { v, s, r in
7+
action(v)
88
}
9-
return self
109
}
1110

1211
@discardableResult
1312
public func onHoverGesture(_ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
14-
if #available(iOS 13.0, *) {
15-
declarativeView.addGestureRecognizer(HoverGestureRecognizer().trackState {
16-
action(self, $0)
17-
})
13+
onHoverGesture { v, s, r in
14+
action(v, s)
1815
}
19-
return self
2016
}
2117

2218
@discardableResult
@@ -32,12 +28,9 @@ extension DeclarativeProtocol {
3228

3329
@discardableResult
3430
public func onHoverGesture(_ state: State<UIGestureRecognizer.State>) -> Self {
35-
if #available(iOS 13.0, *) {
36-
declarativeView.addGestureRecognizer(HoverGestureRecognizer().trackState {
37-
state.wrappedValue = $0
38-
})
31+
onHoverGesture { v, s, r in
32+
state.wrappedValue = s
3933
}
40-
return self
4134
}
4235

4336
@discardableResult
@@ -47,15 +40,12 @@ extension DeclarativeProtocol {
4740

4841
@discardableResult
4942
public func hovered(_ action: @escaping (Bool) -> Void) -> Self {
50-
if #available(iOS 13.0, *) {
51-
declarativeView.addGestureRecognizer(HoverGestureRecognizer().trackState {
52-
let hovered = [.began, .changed].contains($0)
53-
guard hovered != self.properties.hovered else { return }
54-
self.properties.hovered = hovered
55-
action(hovered)
56-
})
43+
onHoverGesture { v, s, r in
44+
let hovered = [.began, .changed].contains(s)
45+
guard hovered != self.properties.hovered else { return }
46+
self.properties.hovered = hovered
47+
action(hovered)
5748
}
58-
return self
5949
}
6050

6151
@discardableResult

Classes/Extensions/DeclarativeProtocol+LongPress.swift

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,60 @@ import UIKit
22

33
extension DeclarativeProtocol {
44
@discardableResult
5-
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (UIGestureRecognizer.State) -> Void) -> Self {
6-
declarativeView.addGestureRecognizer(LongPressGestureRecognizer(taps: taps, touches: touches).trackState(action))
7-
return self
5+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, on state: UIGestureRecognizer.State, _ action: @escaping () -> Void) -> Self {
6+
onLongPressGesture(taps: taps, touches: touches, on: state) { v, r in
7+
action()
8+
}
89
}
910

1011
@discardableResult
11-
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
12-
declarativeView.addGestureRecognizer(LongPressGestureRecognizer(taps: taps, touches: touches).trackState {
13-
action(self, $0)
14-
})
15-
return self
12+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, on state: UIGestureRecognizer.State, _ action: @escaping (Self) -> Void) -> Self {
13+
onLongPressGesture(taps: taps, touches: touches, on: state) { v, r in
14+
action(self)
15+
}
1616
}
1717

1818
@discardableResult
19-
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (Self, UIGestureRecognizer.State, LongPressGestureRecognizer) -> Void) -> Self {
20-
let recognizer = LongPressGestureRecognizer(taps: taps, touches: touches)
21-
declarativeView.addGestureRecognizer(recognizer.trackState {
22-
action(self, $0, recognizer)
23-
})
24-
return self
19+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, on state: UIGestureRecognizer.State, _ action: @escaping (Self, LongPressGestureRecognizer) -> Void) -> Self {
20+
onLongPressGesture(taps: taps, touches: touches) { v, s, r in
21+
if s == state {
22+
action(v, r)
23+
}
24+
}
2525
}
2626

2727
@discardableResult
28-
public func onLongPressGesture(on state: UIGestureRecognizer.State = .began, taps: Int = 0, touches: Int = 1, _ action: @escaping () -> Void) -> Self {
29-
declarativeView.addGestureRecognizer(LongPressGestureRecognizer(taps: taps, touches: touches).trackState {
30-
switch $0 {
31-
case state: action()
32-
default: break
33-
}
34-
})
35-
return self
28+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (Self) -> Void) -> Self {
29+
onLongPressGesture(taps: taps, touches: touches) { v, s, r in
30+
action(v)
31+
}
3632
}
3733

3834
@discardableResult
39-
public func onLongPressGesture(on state: UIGestureRecognizer.State = .began, taps: Int = 0, touches: Int = 1, _ action: @escaping (Self) -> Void) -> Self {
40-
declarativeView.addGestureRecognizer(LongPressGestureRecognizer(taps: taps, touches: touches).trackState {
41-
switch $0 {
42-
case state: action(self)
43-
default: break
44-
}
45-
})
46-
return self
35+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
36+
onLongPressGesture(taps: taps, touches: touches) { v, s, r in
37+
action(v, s)
38+
}
4739
}
4840

4941
@discardableResult
50-
public func onLongPressGesture(_ state: State<UIGestureRecognizer.State>, taps: Int = 0, touches: Int = 1) -> Self {
51-
declarativeView.addGestureRecognizer(LongPressGestureRecognizer(taps: taps, touches: touches).trackState {
52-
state.wrappedValue = $0
42+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ action: @escaping (Self, UIGestureRecognizer.State, LongPressGestureRecognizer) -> Void) -> Self {
43+
let recognizer = LongPressGestureRecognizer(taps: taps, touches: touches)
44+
declarativeView.addGestureRecognizer(recognizer.trackState {
45+
action(self, $0, recognizer)
5346
})
5447
return self
5548
}
49+
50+
@discardableResult
51+
public func onLongPressGesture(taps: Int = 0, touches: Int = 1, _ state: State<UIGestureRecognizer.State>) -> Self {
52+
onLongPressGesture(taps: taps, touches: touches) { v, s, r in
53+
state.wrappedValue = s
54+
}
55+
}
5656

5757
@discardableResult
58-
public func onLongPressGesture<V>(_ expressable: ExpressableState<V, UIGestureRecognizer.State>, taps: Int = 0, touches: Int = 1) -> Self {
59-
onLongPressGesture(expressable.unwrap(), taps: taps, touches: touches)
58+
public func onLongPressGesture<V>(taps: Int = 0, touches: Int = 1, _ expressable: ExpressableState<V, UIGestureRecognizer.State>) -> Self {
59+
onLongPressGesture(taps: taps, touches: touches, expressable.unwrap())
6060
}
6161
}

Classes/Extensions/DeclarativeProtocol+Pan.swift

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,40 @@ import UIKit
22

33
extension DeclarativeProtocol {
44
@discardableResult
5-
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, _ action: @escaping (UIGestureRecognizer.State) -> Void) -> Self {
6-
declarativeView.addGestureRecognizer(PanGestureRecognizer(minTouches: minTouches, maxTouches: maxTouches).trackState(action))
7-
return self
5+
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, on state: UIGestureRecognizer.State, _ action: @escaping () -> Void) -> Self {
6+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches, on: state) { v, r in
7+
action()
8+
}
9+
}
10+
11+
@discardableResult
12+
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self) -> Void) -> Self {
13+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches, on: state) { v, r in
14+
action(self)
15+
}
16+
}
17+
18+
@discardableResult
19+
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self, PanGestureRecognizer) -> Void) -> Self {
20+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches) { v, s, r in
21+
if s == state {
22+
action(v, r)
23+
}
24+
}
25+
}
26+
27+
@discardableResult
28+
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, _ action: @escaping (Self) -> Void) -> Self {
29+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches) { v, s, r in
30+
action(v)
31+
}
832
}
933

1034
@discardableResult
1135
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
12-
declarativeView.addGestureRecognizer(PanGestureRecognizer(minTouches: minTouches, maxTouches: maxTouches).trackState {
13-
action(self, $0)
14-
})
15-
return self
36+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches) { v, s, r in
37+
action(v, s)
38+
}
1639
}
1740

1841
@discardableResult
@@ -25,15 +48,14 @@ extension DeclarativeProtocol {
2548
}
2649

2750
@discardableResult
28-
public func onPanGesture(_ state: State<UIGestureRecognizer.State>, minTouches: Int? = nil, maxTouches: Int? = nil) -> Self {
29-
declarativeView.addGestureRecognizer(PanGestureRecognizer(minTouches: minTouches, maxTouches: maxTouches).trackState {
30-
state.wrappedValue = $0
31-
})
32-
return self
51+
public func onPanGesture(minTouches: Int? = nil, maxTouches: Int? = nil, _ state: State<UIGestureRecognizer.State>) -> Self {
52+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches) { v, s, r in
53+
state.wrappedValue = s
54+
}
3355
}
3456

3557
@discardableResult
36-
public func onPanGesture<V>(_ expressable: ExpressableState<V, UIGestureRecognizer.State>, minTouches: Int? = nil, maxTouches: Int? = nil) -> Self {
37-
onPanGesture(expressable.unwrap(), minTouches: minTouches, maxTouches: maxTouches)
58+
public func onPanGesture<V>(minTouches: Int? = nil, maxTouches: Int? = nil, _ expressable: ExpressableState<V, UIGestureRecognizer.State>) -> Self {
59+
onPanGesture(minTouches: minTouches, maxTouches: maxTouches, expressable.unwrap())
3860
}
3961
}

Classes/Extensions/DeclarativeProtocol+Pinch.swift

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,40 @@ import UIKit
22

33
extension DeclarativeProtocol {
44
@discardableResult
5-
public func onPinchGesture(scale: CGFloat? = nil, _ action: @escaping (UIGestureRecognizer.State) -> Void) -> Self {
6-
declarativeView.addGestureRecognizer(PinchGestureRecognizer(scale: scale).trackState(action))
7-
return self
5+
public func onPinchGesture(scale: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping () -> Void) -> Self {
6+
onPinchGesture(scale: scale, on: state) { v, r in
7+
action()
8+
}
9+
}
10+
11+
@discardableResult
12+
public func onPinchGesture(scale: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self) -> Void) -> Self {
13+
onPinchGesture(scale: scale, on: state) { v, r in
14+
action(self)
15+
}
16+
}
17+
18+
@discardableResult
19+
public func onPinchGesture(scale: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self, PinchGestureRecognizer) -> Void) -> Self {
20+
onPinchGesture(scale: scale) { v, s, r in
21+
if s == state {
22+
action(v, r)
23+
}
24+
}
25+
}
26+
27+
@discardableResult
28+
public func onPinchGesture(scale: CGFloat? = nil, _ action: @escaping (Self) -> Void) -> Self {
29+
onPinchGesture(scale: scale) { v, s, r in
30+
action(v)
31+
}
832
}
933

1034
@discardableResult
1135
public func onPinchGesture(scale: CGFloat? = nil, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
12-
declarativeView.addGestureRecognizer(PinchGestureRecognizer(scale: scale).trackState {
13-
action(self, $0)
14-
})
15-
return self
36+
onPinchGesture(scale: scale) { v, s, r in
37+
action(v, s)
38+
}
1639
}
1740

1841
@discardableResult
@@ -25,15 +48,14 @@ extension DeclarativeProtocol {
2548
}
2649

2750
@discardableResult
28-
public func onPinchGesture(_ state: State<UIGestureRecognizer.State>, scale: CGFloat? = nil) -> Self {
29-
declarativeView.addGestureRecognizer(PinchGestureRecognizer(scale: scale).trackState {
30-
state.wrappedValue = $0
31-
})
32-
return self
51+
public func onPinchGesture(scale: CGFloat? = nil, _ state: State<UIGestureRecognizer.State>) -> Self {
52+
onPinchGesture(scale: scale) { v, s, r in
53+
state.wrappedValue = s
54+
}
3355
}
3456

3557
@discardableResult
36-
public func onPinchGesture<V>(_ expressable: ExpressableState<V, UIGestureRecognizer.State>, scale: CGFloat? = nil) -> Self {
37-
onPinchGesture(expressable.unwrap(), scale: scale)
58+
public func onPinchGesture<V>(scale: CGFloat? = nil, _ expressable: ExpressableState<V, UIGestureRecognizer.State>) -> Self {
59+
onPinchGesture(scale: scale, expressable.unwrap())
3860
}
3961
}

Classes/Extensions/DeclarativeProtocol+Rotation.swift

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,44 @@ import UIKit
22

33
extension DeclarativeProtocol {
44
@discardableResult
5-
public func onRotaionGesture(rotation: CGFloat? = nil, _ action: @escaping (UIGestureRecognizer.State) -> Void) -> Self {
6-
declarativeView.addGestureRecognizer(RotationGestureRecognizer(rotation: rotation).trackState(action))
7-
return self
5+
public func onRotationGesture(rotation: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping () -> Void) -> Self {
6+
onRotationGesture(rotation: rotation, on: state) { v, r in
7+
action()
8+
}
89
}
910

1011
@discardableResult
11-
public func onRotaionGesture(rotation: CGFloat? = nil, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
12-
declarativeView.addGestureRecognizer(RotationGestureRecognizer(rotation: rotation).trackState {
13-
action(self, $0)
14-
})
15-
return self
12+
public func onRotationGesture(rotation: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self) -> Void) -> Self {
13+
onRotationGesture(rotation: rotation, on: state) { v, r in
14+
action(self)
15+
}
16+
}
17+
18+
@discardableResult
19+
public func onRotationGesture(rotation: CGFloat? = nil, on state: UIGestureRecognizer.State, _ action: @escaping (Self, RotationGestureRecognizer) -> Void) -> Self {
20+
onRotationGesture(rotation: rotation) { v, s, r in
21+
if s == state {
22+
action(v, r)
23+
}
24+
}
25+
}
26+
27+
@discardableResult
28+
public func onRotationGesture(rotation: CGFloat? = nil, _ action: @escaping (Self) -> Void) -> Self {
29+
onRotationGesture(rotation: rotation) { v, s, r in
30+
action(v)
31+
}
1632
}
1733

1834
@discardableResult
19-
public func onRotaionGesture(rotation: CGFloat? = nil, _ action: @escaping (Self, UIGestureRecognizer.State, RotationGestureRecognizer) -> Void) -> Self {
35+
public func onRotationGesture(rotation: CGFloat? = nil, _ action: @escaping (Self, UIGestureRecognizer.State) -> Void) -> Self {
36+
onRotationGesture(rotation: rotation) { v, s, r in
37+
action(v, s)
38+
}
39+
}
40+
41+
@discardableResult
42+
public func onRotationGesture(rotation: CGFloat? = nil, _ action: @escaping (Self, UIGestureRecognizer.State, RotationGestureRecognizer) -> Void) -> Self {
2043
let recognizer = RotationGestureRecognizer(rotation: rotation)
2144
declarativeView.addGestureRecognizer(recognizer.trackState {
2245
action(self, $0, recognizer)
@@ -25,15 +48,14 @@ extension DeclarativeProtocol {
2548
}
2649

2750
@discardableResult
28-
public func onRotaionGesture(_ state: State<UIGestureRecognizer.State>, rotation: CGFloat? = nil) -> Self {
29-
declarativeView.addGestureRecognizer(RotationGestureRecognizer(rotation: rotation).trackState {
30-
state.wrappedValue = $0
31-
})
32-
return self
51+
public func onRotationGesture(rotation: CGFloat? = nil, _ state: State<UIGestureRecognizer.State>) -> Self {
52+
onRotationGesture(rotation: rotation) { v, s, r in
53+
state.wrappedValue = s
54+
}
3355
}
3456

3557
@discardableResult
36-
public func onRotaionGesture<V>(_ expressable: ExpressableState<V, UIGestureRecognizer.State>, rotation: CGFloat? = nil) -> Self {
37-
onRotaionGesture(expressable.unwrap(), rotation: rotation)
58+
public func onRotationGesture<V>(rotation: CGFloat? = nil, _ expressable: ExpressableState<V, UIGestureRecognizer.State>) -> Self {
59+
onRotationGesture(rotation: rotation, expressable.unwrap())
3860
}
3961
}

0 commit comments

Comments
 (0)