-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathUITapGestureRecognizer.swift
50 lines (45 loc) · 1.61 KB
/
UITapGestureRecognizer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// UITapGestureRecognizer.swift
// UIKit
//
// Created by Geordie Jay on 30.05.17.
// Copyright © 2017 flowkey. All rights reserved.
//
@MainActor
open class UITapGestureRecognizer: UIGestureRecognizer {
var onTouchesBegan: (() -> Void)?
var onTouchesEnded: (() -> Void)?
var onPress: (@MainActor () -> Void)?
public init(onPress: (@MainActor () -> Void)? = nil) {
self.onPress = onPress
}
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
trackedTouch = touches.first
self.state = .began
onTouchesBegan?()
// run potential cancellation of touches in view and other recognizers
// after `self.state` has been mutated
if cancelsTouchesInView {
trackedTouch?.hasBeenCancelledByAGestureRecognizer = true
}
cancelOtherGestureRecognizersThatShouldNotRecognizeSimultaneously()
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
if let trackedTouch = trackedTouch, touches.first == trackedTouch {
let trackedTouchLocationInView = trackedTouch.location(in: view)
if
let view = self.view, view.point(inside: trackedTouchLocationInView, with: event),
self.state == .began
{
self.state = .recognized
onPress?()
self.state = .possible
} else {
self.state = .failed
}
}
onTouchesEnded?()
}
}