This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
/
PSForceTouchGestureRecognizer.m
79 lines (54 loc) · 1.95 KB
/
PSForceTouchGestureRecognizer.m
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// PSForceTouchGestureRecognizer.h
// Copyright © 2015 Paraset LLC. All rights reserved.
//
#import "PSForceTouchGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface PSForceTouchGestureRecognizer ()
@property (nonatomic, readwrite) CGFloat currentForce;
@end
@implementation PSForceTouchGestureRecognizer
- (id)initWithTarget:(id)target action:(SEL)action {
if (self = [super initWithTarget:target action:action]) {
self.numberOfTouchesRequired = 1.0;
self.minimumForce = 0.0;
}
return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (touches.count != self.numberOfTouchesRequired) {
self.state = UIGestureRecognizerStateFailed;
return;
}
[super touchesBegan:touches withEvent:event];
[super setState:UIGestureRecognizerStateBegan];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGFloat currentMinimumForce = 1.0;
BOOL actionable = YES;
for(UITouch *t in touches) {
CGFloat force = t.force;
CGFloat maxForce = t.maximumPossibleForce;
CGFloat normalizedForce = (force / maxForce);
if(normalizedForce < currentMinimumForce) {
currentMinimumForce = normalizedForce;
}
if(normalizedForce < self.minimumForce) {
actionable = NO;
}
}
self.currentForce = currentMinimumForce;
[super setState:UIGestureRecognizerStateChanged];
if(actionable) {
[super setState:UIGestureRecognizerStateEnded];
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[super setState:UIGestureRecognizerStateCancelled];
}
- (void) touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[super setState:UIGestureRecognizerStateCancelled];
}
@end