resolve conflicts between on-screen buttons & tap gestures

This commit is contained in:
True Zhuanjia committed 2024-08-24 23:29:18 +08:00
1 parent 1d1d4cbddf
commit c6b27ad0e0
4 files changed
+36 -7

No files matched your search

+1 -1
View File
@@ -18,7 +18,7 @@
@property (nonatomic, assign) uint8_t numberOfTouchesRequired;
@property (nonatomic, assign) bool immediateTriggering; // if enabled, trigger the signal on touchesBegan stage.
@property (nonatomic, assign) double tapDownTimeThreshold; // tap down threshold in seconds.
@property (nonatomic, assign) bool areOnScreenButtonTaps; // will be set by the onscreencontrol or other related class
@property (nonatomic, assign) bool areOnScreenControllerTaps; // will be set by the onscreencontrol or other related class
//@property (nonatomic, assign) bool haveOnScreenButtonsOnStreamview;
@property (nonatomic, readonly) CGFloat lowestTouchPointHeight;
@property (nonatomic, readonly) bool gestureCaptured;
+29 -4
View File
@@ -9,9 +9,10 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#import "CustomTapGestureRecognizer.h"
#import "Moonlight-Swift.h"
// The most accurate & reliable tap gesture recognizer of iOS:
// - Almost 100% recoginition rate. UITapGestureRecognizer of Apple API fails frequently, just intractable.
// - Almost 100% recoginition rate. UITapGestureRecognizer of Apple API fails frequently, just a piece of crap.
// - When immediateTriggering is set to false (for native multi-touch):
// Gesture signal will be triggered on touchesEnded stage, multi finger touch operations will not be interrupted by the arising keyboard.
// Instances of different [numberOfTouchesRequired] barely compete with each other, for example, the chance of 3-finger gesture get triggered by 4 or 5 finger tap is very small.
@@ -32,7 +33,7 @@ static CGFloat screenWidthInPoints;
_immediateTriggering = false;
_tapDownTimeThreshold = 0.3;
_gestureCaptured = false;
_areOnScreenButtonTaps = false;
_areOnScreenControllerTaps = false;
return self;
}
@@ -73,14 +74,38 @@ static CGFloat screenWidthInPoints;
_gestureCaptured = false;
self.state = UIGestureRecognizerStateFailed;
}
else if(_gestureCaptured && allTouchesCount == [touches count] && !_areOnScreenButtonTaps){ //must exclude virtual controller taps here to prevent stucked button, _areVirtualControllerTaps flag is set by onscreencontrols or anyother related classes.
else if(_gestureCaptured && allTouchesCount == [touches count] && !_areOnScreenControllerTaps && ![self areOnScreenButtonsTaps]){ //must exclude virtual controller & onscreen button taps here to prevent stucked button, _areVirtualControllerTaps flag is set by onscreencontrols, areOnScreenButtonsTaps will be done by iterating all button views in streamframview
_gestureCaptured = false; //reset for next recognition
if((CACurrentMediaTime() - _gestureCapturedTime) < _tapDownTimeThreshold){
lowestTouchPointYCoord = 0.0; //reset for next recognition
self.state = UIGestureRecognizerStateRecognized;
}
}
if (allTouchesCount == [touches count]) _areOnScreenButtonTaps = false; // need to reset this flag anyway, when all fingers are lefting
if (allTouchesCount == [touches count]) _areOnScreenControllerTaps = false; // need to reset this flag anyway, when all fingers are lefting
}
// it was a not a perfect choice to code OnScreenButtonView in Swift...
// we're unable to import this class to swift codebase by the bridging header,and have to exlucde the onscreen button taps here
// by iterating every button view instances. but it's basically ok since the number of buttonViews are always limited.
// and this method will only be called when the recognizer is active & and the taps passes all the checks and is about to ge triggered.
- (bool)areOnScreenButtonsTaps {
NSTimeInterval t1 = CACurrentMediaTime();
bool gotOneButtonPressed = false;
for(UIView* view in self.view.subviews){
if ([view isKindOfClass:[OnScreenButtonView class]]) {
OnScreenButtonView* buttonView = (OnScreenButtonView*) view;
if(gotOneButtonPressed){ // once we have just 1 button pressed already, reset pressed flag for the all the buttonViews
buttonView.pressed = false; //reset the flag for buttonView
continue;
}
if(buttonView.pressed){
gotOneButtonPressed = true; //got one button pressed
buttonView.pressed = false; // reset the flag for current buttonView
}
}
}
return gotOneButtonPressed;
}
@end
+5 -1
View File
@@ -15,6 +15,7 @@ import UIKit
@objc public var keyLabel: String
@objc public var keyString: String
@objc public var timestamp: TimeInterval
@objc public var pressed: Bool
private let label: UILabel
@@ -27,7 +28,8 @@ import UIKit
self.keyLabel = keyLabel
self.label = UILabel()
self.originalBackgroundColor = UIColor(white: 0.2, alpha: 0.7)
self.timestamp = 0;
self.timestamp = 0
self.pressed = false
super.init(frame: .zero)
setupView()
}
@@ -104,6 +106,7 @@ import UIKit
// Touch event handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.pressed = true
super.touchesBegan(touches, with: event)
//self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 0.7)
@@ -161,6 +164,7 @@ import UIKit
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// self.pressed = false // will be reset outside the class
super.touchesEnded(touches, with: event)
if !OnScreenButtonView.editMode && !self.keyString.contains("+") { // if the command(keystring contains "+", it's a multi-key command rather than a single key button
+1 -1
View File
@@ -1100,7 +1100,7 @@ static float L3_Y;
if ([gesture isKindOfClass:[CustomTapGestureRecognizer class]]) {
// This is a CustomTapGestureRecognizer
CustomTapGestureRecognizer *tapGesture = (CustomTapGestureRecognizer *)gesture;
tapGesture.areOnScreenButtonTaps = true;
tapGesture.areOnScreenControllerTaps = true;
// Perform actions with tapGesture
}
}