Cancel gesture after 1 second timeout; cleanup

This commit is contained in:
Bryce Hackel 2024-09-04 00:19:40 -07:00
parent bbee22218a
commit b9edf2bccd
No known key found for this signature in database
GPG key ID: F031960F08455E88

View file

@ -749,6 +749,8 @@ BOOL isTabSelected = NO;
static CGFloat deadzoneStartingXTranslation; static CGFloat deadzoneStartingXTranslation;
// Variable to track the X translation of the pan gesture after exiting the deadzone // Variable to track the X translation of the pan gesture after exiting the deadzone
static CGFloat adjustedTranslationX; static CGFloat adjustedTranslationX;
// Variable to cancel gesture if deadzone is not exited after a certain time
static NSDate *gestureStartTime;
// Constant for the deadzone radius that can be changed in the settings // Constant for the deadzone radius that can be changed in the settings
static CGFloat deadzoneRadius = (CGFloat)GetFloat(@"playerGesturesDeadzone"); static CGFloat deadzoneRadius = (CGFloat)GetFloat(@"playerGesturesDeadzone");
// Constant for the sensitivity factor that can be changed in the settings // Constant for the sensitivity factor that can be changed in the settings
@ -759,6 +761,7 @@ BOOL isTabSelected = NO;
// Get objects that should only be initialized once // Get objects that should only be initialized once
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
// Get objects for adjusting Volume
volumeView = [[MPVolumeView alloc] init]; volumeView = [[MPVolumeView alloc] init];
for (UIView *view in volumeView.subviews) { for (UIView *view in volumeView.subviews) {
if ([view isKindOfClass:[UISlider class]]) { if ([view isKindOfClass:[UISlider class]]) {
@ -766,6 +769,7 @@ BOOL isTabSelected = NO;
break; break;
} }
} }
// Initialize the haptic feedback generator
feedbackGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium]; feedbackGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
}); });
// Get objects used to seek nicely in the video player // Get objects used to seek nicely in the video player
@ -846,14 +850,9 @@ BOOL isTabSelected = NO;
[playerBarController startScrubbing]; [playerBarController startScrubbing];
break; break;
case GestureModeDisabled: case GestureModeDisabled:
// Do nothing if the gesture is disabled // Fall through
break;
default: default:
// Show an alert if the gesture mode is invalid // Do nothing
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Invalid Gesture Mode" message:@"Please report this bug." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
break; break;
} }
}; };
@ -874,14 +873,10 @@ BOOL isTabSelected = NO;
adjustSeek(adjustedTranslationX, initialTime); adjustSeek(adjustedTranslationX, initialTime);
break; break;
case GestureModeDisabled: case GestureModeDisabled:
// Do nothing if the gesture is disabled // Fall through
break; break;
default: default:
// Show an alert if the gesture mode is invalid // Do nothing
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Invalid Gesture Mode" message:@"Please report this bug." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
break; break;
} }
}; };
@ -900,13 +895,9 @@ BOOL isTabSelected = NO;
[playerBarController endScrubbingForSeekSource:0]; [playerBarController endScrubbingForSeekSource:0];
break; break;
case GestureModeDisabled: case GestureModeDisabled:
break; // Fall through
default: default:
// Show an alert if the gesture mode is invalid // Do nothing
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Invalid Gesture Mode" message:@"Please report this bug." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
break; break;
} }
}; };
@ -936,13 +927,8 @@ BOOL isTabSelected = NO;
} }
// Deactive the activity flag // Deactive the activity flag
isValidHorizontalPan = NO; isValidHorizontalPan = NO;
// Cancel this gesture if it has not activated after 1 second // Store current time for gesture timeout
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ gestureStartTime = [NSDate date];
if (!isValidHorizontalPan && panGestureRecognizer.state != UIGestureRecognizerStateEnded) {
// Cancel the gesture by setting its state to UIGestureRecognizerStateCancelled
panGestureRecognizer.state = UIGestureRecognizerStateCancelled;
}
});
} }
// Handle changed gesture state by activating the gesture once it has exited the deadzone, // Handle changed gesture state by activating the gesture once it has exited the deadzone,
@ -951,6 +937,15 @@ BOOL isTabSelected = NO;
// Determine if the gesture is predominantly horizontal // Determine if the gesture is predominantly horizontal
CGPoint translation = [panGestureRecognizer translationInView:self.view]; CGPoint translation = [panGestureRecognizer translationInView:self.view];
if (!isValidHorizontalPan) { if (!isValidHorizontalPan) {
// Timeout
// Check how much time has passed since the gesture started
NSTimeInterval timeElapsed = [[NSDate date] timeIntervalSinceDate:gestureStartTime];
// If more than 1 second has passed and the gesture hasn't activated, cancel the gesture
if (timeElapsed >= 1.0 && !isValidHorizontalPan) {
panGestureRecognizer.state = UIGestureRecognizerStateCancelled;
}
// Horizontal and deadzone check
if (fabs(translation.x) > fabs(translation.y)) { if (fabs(translation.x) > fabs(translation.y)) {
// Check if the touch has moved outside the deadzone // Check if the touch has moved outside the deadzone
CGFloat distanceFromStart = hypot(translation.x, translation.y); CGFloat distanceFromStart = hypot(translation.x, translation.y);