fix onscreenControl Stuck

This commit is contained in:
True Zhuanjia committed 2024-06-25 02:08:43 +08:00
1 parent a5c8224023
commit aae87d1684
2 files changed
+12 -1

No files matched your search

@@ -20,6 +20,7 @@
@property (nonatomic, assign) double tapDownTimeThreshold; // tap down threshold in seconds. @property (nonatomic, assign) double tapDownTimeThreshold; // tap down threshold in seconds.
@property (nonatomic, readonly) CGFloat lowestTouchPointHeight; @property (nonatomic, readonly) CGFloat lowestTouchPointHeight;
@property (nonatomic, readonly) bool gestureCaptured; @property (nonatomic, readonly) bool gestureCaptured;
@property (nonatomic, readonly) bool areVirtualControllerTaps;
@property (nonatomic, readonly) NSTimeInterval gestureCapturedTime; @property (nonatomic, readonly) NSTimeInterval gestureCapturedTime;
@end @end
+11 -1
View File
@@ -21,15 +21,18 @@
@implementation CustomTapGestureRecognizer @implementation CustomTapGestureRecognizer
static CGFloat screenHeightInPoints; static CGFloat screenHeightInPoints;
static CGFloat screenWidthInPoints;
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action { - (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action {
self = [super initWithTarget:target action:action]; self = [super initWithTarget:target action:action];
screenHeightInPoints = CGRectGetHeight([[UIScreen mainScreen] bounds]); screenHeightInPoints = CGRectGetHeight([[UIScreen mainScreen] bounds]);
screenWidthInPoints = CGRectGetWidth([[UIScreen mainScreen] bounds]);
lowestTouchPointYCoord = 0.0; lowestTouchPointYCoord = 0.0;
_numberOfTouchesRequired = 3; _numberOfTouchesRequired = 3;
_immediateTriggering = false; _immediateTriggering = false;
_tapDownTimeThreshold = 0.3; _tapDownTimeThreshold = 0.3;
_gestureCaptured = false; _gestureCaptured = false;
_areVirtualControllerTaps = false;
return self; return self;
} }
@@ -41,6 +44,12 @@ static CGFloat screenHeightInPoints;
for(UITouch *touch in [event allTouches]){ for(UITouch *touch in [event allTouches]){
if(lowestTouchPointYCoord < [touch locationInView:self.view].y) lowestTouchPointYCoord = [touch locationInView:self.view].y; if(lowestTouchPointYCoord < [touch locationInView:self.view].y) lowestTouchPointYCoord = [touch locationInView:self.view].y;
} }
if(_numberOfTouchesRequired == 2){
NSArray *twoTouches = [[event allTouches] allObjects];
_areVirtualControllerTaps = fabs([twoTouches[1] locationInView:self.view].x - [twoTouches[0] locationInView:self.view].x) > screenWidthInPoints/3;
}
_lowestTouchPointHeight = screenHeightInPoints - lowestTouchPointYCoord; _lowestTouchPointHeight = screenHeightInPoints - lowestTouchPointYCoord;
if(_immediateTriggering){ if(_immediateTriggering){
lowestTouchPointYCoord = 0.0; //reset for next recoginition lowestTouchPointYCoord = 0.0; //reset for next recoginition
@@ -61,7 +70,8 @@ static CGFloat screenHeightInPoints;
if([[event allTouches] count] > _numberOfTouchesRequired) { if([[event allTouches] count] > _numberOfTouchesRequired) {
_gestureCaptured = false; _gestureCaptured = false;
self.state = UIGestureRecognizerStateFailed; self.state = UIGestureRecognizerStateFailed;
} else if(_gestureCaptured && [[event allTouches] count] == [touches count]){ }
else if(_gestureCaptured && [[event allTouches] count] == [touches count] && !_areVirtualControllerTaps){ //must exclude virtual controller taps here to prevent stucked button
_gestureCaptured = false; //reset for next recognition _gestureCaptured = false; //reset for next recognition
if((CACurrentMediaTime() - _gestureCapturedTime) < _tapDownTimeThreshold){ if((CACurrentMediaTime() - _gestureCapturedTime) < _tapDownTimeThreshold){
lowestTouchPointYCoord = 0.0; //reset for next recognition lowestTouchPointYCoord = 0.0; //reset for next recognition