Attempt brightness and volume gestures

This commit is contained in:
Bryce Hackel
2024-08-09 16:51:12 -07:00
parent 91290efdfe
commit 64c712d630

View File

@@ -576,17 +576,32 @@ BOOL isTabSelected = NO;
%new
- (void)YTLitePlusHandlePanGesture:(UIPanGestureRecognizer *)panGestureRecognizer {
static float initialVolume;
static float initialBrightness;
static BOOL isHorizontalPan = NO;
static NSInteger gestureSection = 0; // 1 for brightness, 2 for volume, 3 for seeking
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
// Store the initial volume at the beginning of the pan gesture
// Get the gesture's start position
CGPoint startLocation = [panGestureRecognizer locationInView:self.view];
CGFloat viewHeight = self.view.bounds.size.height;
// Determine the section based on the start position
if (startLocation.y <= viewHeight / 3.0) {
gestureSection = 1; // Top third: Brightness
initialBrightness = [UIScreen mainScreen].brightness;
} else if (startLocation.y <= 2 * viewHeight / 3.0) {
gestureSection = 2; // Middle third: Volume
initialVolume = [[AVAudioSession sharedInstance] outputVolume];
} else {
gestureSection = 3; // Bottom third: Seeking
}
// Reset the horizontal pan flag
isHorizontalPan = NO;
}
if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
// Calculate the translation
// Calculate the translation of the gesture
CGPoint translation = [panGestureRecognizer translationInView:self.view];
// Determine if the gesture is predominantly horizontal
@@ -600,13 +615,19 @@ BOOL isTabSelected = NO;
}
}
// Handle the gesture based on the identified section
if (isHorizontalPan) {
float newVolume = initialVolume + (translation.x / 1000.0); // Adjust the divisor to control the sensitivity
if (gestureSection == 1) {
// Top third: Adjust brightness
float newBrightness = initialBrightness + (translation.x / 1000.0);
newBrightness = fmaxf(fminf(newBrightness, 1.0), 0.0);
[[UIScreen mainScreen] setBrightness:newBrightness];
// Clamp the volume between 0 and 1
} else if (gestureSection == 2) {
// Middle third: Adjust volume
float newVolume = initialVolume + (translation.x / 1000.0);
newVolume = fmaxf(fminf(newVolume, 1.0), 0.0);
// https://stackoverflow.com/questions/50737943/how-to-change-volume-programmatically-on-ios-11-4/50740074#50740074
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider *volumeViewSlider = nil;
for (UIView *view in volumeView.subviews) {
@@ -618,12 +639,18 @@ BOOL isTabSelected = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
volumeViewSlider.value = newVolume;
});
} else if (gestureSection == 3) {
// Bottom third: Seek functionality (implement your seeking logic here)
// Example: Adjust playback position based on horizontal swipe
// (Add your media player's seeking logic here)
}
}
}
if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
if (isHorizontalPan) {
// Haptic feedback
// Provide haptic feedback upon successful gesture recognition
UINotificationFeedbackGenerator *feedbackGenerator = [[UINotificationFeedbackGenerator alloc] init];
[feedbackGenerator notificationOccurred:UINotificationFeedbackTypeSuccess];
feedbackGenerator = nil;