播放器上下滑动调节音量,左右滑动调节进度的实现方案

在实现播放器的过程中,可能需要在滑动屏幕时要实现左右滑动快进快退,上下滑动调节音量的需求,我的实现方案是只添加一个<code>
UIPanGestureRecognizer</code>进行实现.
下面说说解决方案吧

1 添加手势

    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [self addGestureRecognizer:recognizer];

2.判断滑动的方向

2.1 添加一个最小变化
添加一个<code>gestureMinimumTranslation</code>用于判断方向
<code>
CGFloat const gestureMinimumTranslation = 5.0;</code>
2.2定义一个枚举变量,标识方向

typedef enum : NSInteger{
    PanDirectionNone,
    PanDirectionUp,
    PanDirectionDown,
    PanDirectionRight,
    PanDirectionLeft
} PanDirection;

@property (nonatomic, assign) PanDirection  direction;

2.3 定义方法,判断滑动的方向

// This method will determine whether the direction of the user's swipe
- (PanDirection)determinePanDirectionIfNeeded:(CGPoint)translation
{
    if (self.direction != PanDirectionNone)
        return self.direction;
    if (fabs(translation.x) > gestureMinimumTranslation)
    {
        BOOL gestureHorizontal = NO;
        if (translation.y ==0.0)
            gestureHorizontal = YES;
        else
            gestureHorizontal = (fabs(translation.x / translation.y) >5.0);
        if (gestureHorizontal)
        {
            if (translation.x >0.0)
                return PanDirectionRight;
            else
                return PanDirectionLeft;
        }
    }
    else if (fabs(translation.y) > gestureMinimumTranslation)
    {
        BOOL gestureVertical = NO;
        if (translation.x ==0.0)
            gestureVertical = YES;
        else
            gestureVertical = (fabs(translation.y / translation.x) >5.0);
        if (gestureVertical)
        {
            if (translation.y >0.0)
                return PanDirectionDown;
            else
                return PanDirectionUp;
        }
    }
    return self.direction;
}

2.3实现滑动方法


- (void)handleSwipe:(UIPanGestureRecognizer *)gesture
{
    
    CGFloat widthSeconds = 90;
    
    
    CGFloat percentLength = widthSeconds / self.view.frame.size.width;
    
    CGFloat panDirectionY = [gesture velocityInView:gesture.view].y;
    CGPoint translation = [gesture translationInView:gesture.view];
    if (gesture.state ==UIGestureRecognizerStateBegan)
    {
        self.direction = PanDirectionNone;
        self.startPoint = [gesture locationInView:gesture.view];
        
    }
    if (gesture.state == UIGestureRecognizerStateChanged )
    {
        self.direction = [self determinePanDirectionIfNeeded:translation];
        
        switch (self.direction) {
            case PanDirectionUp:
            case PanDirectionDown:
            {
                self.isChangingVolume = YES;
                //
                if (panDirectionY > 0) {
                    
                    
                    MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
                    musicController.volume -= 0.01;
                }
                else{
                    MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
                    musicController.volume += 0.01;
                    
                }
                
                break;
            }
                
            case PanDirectionRight: // 向右划(快进)
            {
                
                
                // 此时可以根据滑动的时间,在界面上进行显示快进,快退时间
                
                CGPoint endPoint = [gesture locationInView:gesture.view];
                int panLength = endPoint.x - self.startPoint.x;
                if (panLength > 0) {
                    // 计算滑动的距离显示
                    NSInteger timeLength = panLength * percentLength;
                    NSLog(@"%zd",timeLength);
                }else{
                    //       // 向右划的过程中,向左划,而且比初始位置还要向左
                    CGPoint endPoint = [gesture locationInView:gesture.view];
                    int panLength = endPoint.x - self.startPoint.x;
                    int timeLength = panLength * percentLength * -1;
                    
                    NSLog(@"%zd",timeLength);
                }
                
                break;
            }
            case PanDirectionLeft: // 向左划 快退手势
            {
                
                
                // 此时可以根据滑动的时间,在界面上进行显示快进,快退时间
                CGPoint endPoint = [gesture locationInView:gesture.view];
                int panLength = endPoint.x - self.startPoint.x;
                if (panLength <  0) {
                    
                    int timeLength = panLength * percentLength * -1;
                    NSLog(@"%zd",timeLength);
                }else{ //     向左划的过程中,向右划,而且比初始位置还要向右
                    
                    CGPoint endPoint = [gesture locationInView:gesture.view];
                    int panLength = endPoint.x - self.startPoint.x;
                    
                    int timeLength = panLength * percentLength;
                    NSLog(@"%zd",timeLength);
                }
            }
                
            default:
                break;
        }
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
       
        
        if (!self.isChangingVolume) { // 如果不是改变声音
            
            CGPoint endPoint = [gesture locationInView:gesture.view];
            CGFloat panLength = endPoint.x - self.startPoint.x;
            
            NSLog(@"%f",panLength);
            CGFloat seconds = panLength *  percentLength ;
            
            // 此时计算出快进/快退的时间,可以让播放器实现快进快退
            
        }
        self.isChangingVolume = NO;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,939评论 25 708
  • 尊重知识,转发请注明出处:iOS流媒体开发之二:滑动手势控制音量、亮度和进度 概要 看到文章的标题,小伙伴们大概会...
    张云龙阅读 10,683评论 26 94
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 昨天学习了阿何老师的思维导图课的第2课,主要学习了用思维导图做记录、列大纲。 第2课从总结归纳、文章笔记、会议记录...
    德飞商学院阅读 349评论 0 1
  • 洛城的秋天总是来得格外早。 枯黄的残叶拍打着车窗哗哗作响。映射在玻璃后面的,是一张因为常年不见阳光而略显苍白...
    落落寒雨阅读 181评论 0 0