From 82de44df54563f6f20b587aa195e0ad5dfa9d873 Mon Sep 17 00:00:00 2001 From: True Zhuanjia Date: Tue, 14 May 2024 23:05:58 +0800 Subject: [PATCH] Rewrite CustomEdgeSwipeGestureRecognizer to fix unexpected session disconnect, and for future configuration --- .../CustomEdgeSwipeGestureRecognizer.h | 3 + .../CustomEdgeSwipeGestureRecognizer.m | 71 +++++++++---------- .../StreamFrameViewController.m | 4 +- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h index 4453b5f0..4e4ef2b0 100644 --- a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h +++ b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.h @@ -13,5 +13,8 @@ @interface CustomEdgeSwipeGestureRecognizer : UIGestureRecognizer +@property (nonatomic, assign) UIRectEdge edges; // Specify the edge(s) you want to recognize the swipe gesture on +@property (nonatomic, assign) CGFloat normalizedThresholdDistance; // Distance from the edge to start recognizing the gesture + @end #endif /* CustomEdgeSwipeGestureRecognizer_h */ diff --git a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m index d60c1fe9..af4214c0 100644 --- a/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m +++ b/Limelight/ViewControllers/CustomEdgeSwipeGestureRecognizer.m @@ -11,53 +11,52 @@ #import "CustomEdgeSwipeGestureRecognizer.h" #import -@implementation CustomEdgeSwipeGestureRecognizer { - CGPoint _startPoint; - CGFloat _nomarlizedswipeThreshold; +@implementation CustomEdgeSwipeGestureRecognizer +UITouch* capturedUITouch; +CGFloat _startPointX; +static CGFloat screenWidthInPoints; +static CGFloat EDGE_TOLERANCE_POINTS = 50.0f; + +- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action { + self = [super initWithTarget:target action:action]; + screenWidthInPoints = CGRectGetWidth([[UIScreen mainScreen] bounds]); // Get the screen's bounds (in points) + return self; } - (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); + capturedUITouch = touch; + _startPointX = [capturedUITouch locationInView:self.view].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 touchesEnded:touches withEvent:event]; - [super touchesMoved:touches withEvent:event]; - UITouch *touch = [touches anyObject]; - CGPoint gestureEndPoint = [touch locationInView:self.view]; - CGFloat nomarlizedGestureEndPointX = gestureEndPoint.x/screenWidthInPoints; - CGFloat nomarlizedStartPointX = _startPoint.x/screenWidthInPoints; - // CGFloat nomarlizedgestureEndPointYDelta = (gestureEndPoint.y-_startPoint.y)/screenHeightInPoints; - // Log(LOG_I, @"current point x: %f", nomarlizedgestureEndPointXDelta); - // NSLog(@"in CUSTOM swipe REC"); - // NSLog(@"startpointX %f", _startPoint.x); - // NSLog(@"NorXDelta %f", nomarlizedGestureEndPointX); - _nomarlizedswipeThreshold = 0.5; // You need swipe half of screen width from left edge to trigger this Recognizer - if (nomarlizedStartPointX <= 0.025 && nomarlizedGestureEndPointX > _nomarlizedswipeThreshold) { - // Detected a swipe from the left edge that exceeds _nomarlizedswipeThreshold - if (self.state == UIGestureRecognizerStatePossible) { - self.state = UIGestureRecognizerStateBegan; + if([touches containsObject:capturedUITouch]){ + CGFloat _endPointX = [capturedUITouch locationInView:self.view].x; + CGFloat normalizedGestureDistance = fabs(_endPointX - _startPointX)/screenWidthInPoints; + + if(self.edges & UIRectEdgeLeft){ + if(_startPointX < EDGE_TOLERANCE_POINTS && normalizedGestureDistance > _normalizedThresholdDistance){ + self.state = UIGestureRecognizerStateBegan; + self.state = UIGestureRecognizerStateEnded; + } + // NSLog(@"_startPointX %f , normalizedGestureDeltaX %f", _startPointX, normalizedGestureDistance); + } + if(self.edges & UIRectEdgeRight){ + if((_startPointX > (screenWidthInPoints - EDGE_TOLERANCE_POINTS)) && normalizedGestureDistance > _normalizedThresholdDistance){ + self.state = UIGestureRecognizerStateBegan; + self.state = UIGestureRecognizerStateEnded; + } + // NSLog(@"_startPointX %f , normalizedGestureDeltaX %f", _startPointX, normalizedGestureDistance); } - } 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 e6065b96..9a859048 100644 --- a/Limelight/ViewControllers/StreamFrameViewController.m +++ b/Limelight/ViewControllers/StreamFrameViewController.m @@ -133,8 +133,8 @@ #else _exitSwipeRecognizer = [[CustomEdgeSwipeGestureRecognizer alloc] initWithTarget:self action:@selector(edgeSwiped)]; - //_exitSwipeRecognizer.edges = UIRectEdgeLeft; - //_exitSwipeRecognizer.edges = UIRectEdgeLeft; + _exitSwipeRecognizer.edges = UIRectEdgeLeft; + _exitSwipeRecognizer.normalizedThresholdDistance = 0.5f; _exitSwipeRecognizer.delaysTouchesBegan = NO; _exitSwipeRecognizer.delaysTouchesEnded = NO; [self.view addGestureRecognizer:_exitSwipeRecognizer];