From 616238e84799975ebfa78f8201663fe25917afb2 Mon Sep 17 00:00:00 2001 From: ZWM <> Date: Tue, 30 Apr 2024 23:41:58 +0800 Subject: [PATCH] Add a custom gesture recognizer to trigger session disconnection when user swipe with a horizontal distance that exceeds half screen width, this works better in native-touch passthrough. Original ios-moonlight triggers session disconnection with a short swipe from left edge, which happens frequently & unexpectedly in an intense native-touch passthrough gaming condition (like GenshinImpact). --- .../CustomEdgeSwipeGestureRecognizer.h | 17 ++++++ .../CustomEdgeSwipeGestureRecognizer.m | 60 +++++++++++++++++++ .../StreamFrameViewController.m | 12 ++-- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h create mode 100644 Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m diff --git a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h new file mode 100644 index 00000000..4453b5f0 --- /dev/null +++ b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h @@ -0,0 +1,17 @@ +// +// CustomEdgeSwipeGestureRecognizer.h +// Moonlight-ZWM +// +// Created by ZWM on 2024/4/30. +// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved. +// + +#ifndef CustomEdgeSwipeGestureRecognizer_h +#define CustomEdgeSwipeGestureRecognizer_h +// CustomEdgeSwipeGestureRecognizer.h +#import + +@interface CustomEdgeSwipeGestureRecognizer : UIGestureRecognizer + +@end +#endif /* CustomEdgeSwipeGestureRecognizer_h */ diff --git a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m new file mode 100644 index 00000000..e8ec9d9b --- /dev/null +++ b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m @@ -0,0 +1,60 @@ +// +// CustomEdgeSwipeGestureRecognizer.m +// Moonlight-ZWM +// +// Created by ZWM on 2024/4/30. +// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved. +// + +// #import + +#import "CustomEdgeSwipeGestureRecognizer.h" +#import + +@implementation CustomEdgeSwipeGestureRecognizer { + CGPoint _startPoint; + CGFloat _nomarlizedswipeThreshold; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + [super touchesBegan:touches withEvent:event]; + + UITouch *touch = [touches anyObject]; + _startPoint = [touch locationInView:self.view]; + // Log(LOG_I, @"start point x: %f", _startPoint.x); +} + + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + CGRect screenBounds = [[UIScreen mainScreen] bounds];// Get the screen's bounds (in points) + CGFloat screenWidthInPoints = CGRectGetWidth(screenBounds); + // CGFloat screenHeightInPoints = CGRectGetHeight(screenBounds); + // Log the screen resolution + // NSLog(@"Screen resolution: %.0f x %.0f points", screenWidthInPoints, screenHeightInPoints); + + + [super touchesMoved:touches withEvent:event]; + UITouch *touch = [touches anyObject]; + CGPoint gestureEndPoint = [touch locationInView:self.view]; + CGFloat nomarlizedgestureEndPointXDelta = (gestureEndPoint.x-_startPoint.x)/screenWidthInPoints; + // CGFloat nomarlizedgestureEndPointYDelta = (gestureEndPoint.y-_startPoint.y)/screenHeightInPoints; + // Log(LOG_I, @"current point x: %f", nomarlizedgestureEndPointXDelta); + + _nomarlizedswipeThreshold = 0.5; // You need swipe half of screen width from left edge to trigger this Recognizer + if (_startPoint.x == 0.0 && nomarlizedgestureEndPointXDelta > _nomarlizedswipeThreshold) { + // Detected a swipe from the left edge that exceeds _nomarlizedswipeThreshold + if (self.state == UIGestureRecognizerStatePossible) { + self.state = UIGestureRecognizerStateBegan; + } + } else { + self.state = UIGestureRecognizerStateFailed; + } +} + +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { + [super touchesCancelled:touches withEvent:event]; + + self.state = UIGestureRecognizerStateFailed; +} + +@end diff --git a/Limelight/ViewControllers/StreamFrameViewController.m b/Limelight/ViewControllers/StreamFrameViewController.m index facefbdc..a02ffac3 100644 --- a/Limelight/ViewControllers/StreamFrameViewController.m +++ b/Limelight/ViewControllers/StreamFrameViewController.m @@ -17,6 +17,7 @@ #include #include #include +#import "CustomEdgeSwipeGestureRecognizer.h" #if TARGET_OS_TV #import @@ -30,6 +31,8 @@ - (id)initWithRefreshRate:(float)arg1 videoDynamicRange:(int)arg2; @end + + @implementation StreamFrameViewController { ControllerSupport *_controllerSupport; StreamManager *_streamMan; @@ -49,7 +52,7 @@ CGSize _keyboardSize; #if !TARGET_OS_TV - UIScreenEdgePanGestureRecognizer *_exitSwipeRecognizer; + CustomEdgeSwipeGestureRecognizer *_exitSwipeRecognizer; #endif } @@ -129,12 +132,13 @@ [self.view addGestureRecognizer:_playPauseTapGestureRecognizer]; #else - _exitSwipeRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeSwiped)]; - _exitSwipeRecognizer.edges = UIRectEdgeLeft; + _exitSwipeRecognizer = [[CustomEdgeSwipeGestureRecognizer alloc] initWithTarget:self action:@selector(edgeSwiped)]; + //_exitSwipeRecognizer.edges = UIRectEdgeLeft; + //_exitSwipeRecognizer.edges = UIRectEdgeLeft; _exitSwipeRecognizer.delaysTouchesBegan = NO; _exitSwipeRecognizer.delaysTouchesEnded = NO; - // [self.view addGestureRecognizer:_exitSwipeRecognizer]; + [self.view addGestureRecognizer:_exitSwipeRecognizer]; #endif _tipLabel = [[UILabel alloc] init];