iOS 滑动调节音量和亮度

typedef NS_ENUM(NSInteger, MyPanType) {
    MyPanTypeNone,
    MyPanTypeBrightness,
    MyPanTypeVolume,
};

- (void)initGestureProcessor {
    _currentPanType = MyPanTypeNone;
    
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
    [_panGestureRecognizer setMinimumNumberOfTouches:1];
    [_panGestureRecognizer setMaximumNumberOfTouches:1];
    _panGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:_panGestureRecognizer];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.view)
        return NO;
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (MyPanType)detectPanTypeForPan:(UIPanGestureRecognizer *)panRecognizer {
    CGPoint location = [panRecognizer locationInView:self.view];
    CGFloat position = location.x;
    CGFloat viewWidth = 0.0;
    viewWidth = self.view.frame.size.width;
    
    MyPanType panType = MyPanTypeVolume;
    if (position < viewWidth / 2)
        panType = MyPanTypeBrightness;
    
    return panType;
}

- (void)panGestureRecognized:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGFloat panDirectionY = [panGestureRecognizer velocityInView:self.view].y;
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan)
        _currentPanType = [self detectPanTypeForPan:panGestureRecognizer];
    
    if (_currentPanType == MyPanTypeVolume) {
        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
        if (panDirectionY > 0)
            musicPlayer.volume -= 0.01;
        else
            musicPlayer.volume += 0.01;
        
    } else if (_currentPanType == MyPanTypeBrightness) {
        CGFloat brightness = [UIScreen mainScreen].brightness;
        if (panDirectionY > 0)
            brightness -= 0.01;
        else
            brightness += 0.01;
        
        if (brightness > 1.0)
            brightness += 1.0;
        else if (brightness < 0.0)
            brightness = 0.0;
        [[UIScreen mainScreen] setBrightness:brightness];
    }
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        _currentPanType = MyPanTypeNone;
    }
}