71 lines
2.8 KiB
Objective-C
71 lines
2.8 KiB
Objective-C
//
|
|
// CustomTapGestureRecognizer.m
|
|
// Moonlight
|
|
//
|
|
// Created by ZWM on 2024/5/15.
|
|
// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
|
|
//
|
|
|
|
#import <Foundation/Foundation.h>
|
|
#import <UIKit/UIGestureRecognizerSubclass.h>
|
|
#import "CustomTapGestureRecognizer.h"
|
|
|
|
// The most accurate & reliable tap gesture recognizer of iOS:
|
|
// - almost 100% recoginition rate. UITapGestureRecognizer of Apple API is just intractable.
|
|
// - instances of different numberOfTouchesRequired barely compete with each other, even when immediateTriggering is set to false.
|
|
// - set property immediateTriggering = true, ensures no competition with gestures of less fingers in non-native touch mode.
|
|
|
|
@implementation CustomTapGestureRecognizer
|
|
|
|
static CGFloat screenHeightInPoints;
|
|
|
|
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action {
|
|
self = [super initWithTarget:target action:action];
|
|
screenHeightInPoints = CGRectGetHeight([[UIScreen mainScreen] bounds]);
|
|
lowestTouchPointYCoord = 0.0;
|
|
_numberOfTouchesRequired = 3;
|
|
_immediateTriggering = false;
|
|
_tapDownTimeThreshold = 0.3;
|
|
_gestureCaptured = false;
|
|
return self;
|
|
}
|
|
|
|
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
|
// Check if the number of touches and taps meets the required criteria
|
|
if ([[event allTouches] count] == _numberOfTouchesRequired) {
|
|
_gestureCapturedTime = CACurrentMediaTime();
|
|
_gestureCaptured = true;
|
|
for(UITouch *touch in [event allTouches]){
|
|
if(lowestTouchPointYCoord < [touch locationInView:self.view].y) lowestTouchPointYCoord = [touch locationInView:self.view].y;
|
|
}
|
|
_lowestTouchPointHeight = screenHeightInPoints - lowestTouchPointYCoord;
|
|
if(_immediateTriggering){
|
|
lowestTouchPointYCoord = 0.0; //reset for next recoginition
|
|
self.state = UIGestureRecognizerStateRecognized;
|
|
return;
|
|
}
|
|
self.state = UIGestureRecognizerStatePossible;
|
|
}
|
|
if ([[event allTouches] count] > _numberOfTouchesRequired) {
|
|
_gestureCaptured = false;
|
|
self.state = UIGestureRecognizerStateFailed;
|
|
}
|
|
}
|
|
|
|
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
|
// [super touchesEnded:touches withEvent:event];
|
|
if(_immediateTriggering) return;
|
|
if([[event allTouches] count] > _numberOfTouchesRequired) {
|
|
_gestureCaptured = false;
|
|
self.state = UIGestureRecognizerStateFailed;
|
|
} else if(_gestureCaptured && [[event allTouches] count] == [touches count]){
|
|
_gestureCaptured = false; //reset for next recognition
|
|
if((CACurrentMediaTime() - _gestureCapturedTime) < _tapDownTimeThreshold){
|
|
lowestTouchPointYCoord = 0.0; //reset for next recognition
|
|
self.state = UIGestureRecognizerStateRecognized;
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|