iOS 手勢(shì)滑動(dòng)改變屏幕聲音和亮度 和播放進(jìn)度

之前做了一個(gè)關(guān)于視頻播放的項(xiàng)目,需求是 當(dāng)屏幕橫屏?xí)r, 上下滑動(dòng)屏幕左可以改變屏幕亮度,上下滑動(dòng)屏幕右側(cè)可以改變屏幕音量。具體的實(shí)現(xiàn)如下:
首先 自定義亮度和音量展示UI

 UIImage *blightImage = [UIImage imageNamed:@"blight"];
    UIImage *voiceImage = [UIImage imageNamed:@"volume"];
    
    blightView =[[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_HEIGHT-150)/2,(SCREEN_WIDTH-150)/2, 150, 150)];
    blightView.image =blightImage;
    blightView.alpha=0.0;
    blightView.backgroundColor =[UIColor clearColor];
    [self addSubview:blightView];
    
    voiceView =[[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_HEIGHT-150)/2,(SCREEN_WIDTH-150)/2, 150, 150)];
    voiceView.image =voiceImage;
    voiceView.alpha=0.0;
    voiceView.backgroundColor =[UIColor clearColor];
    [self addSubview:voiceView];

在ImageView上添加 ProgressView

 blightPtogress =[[UIProgressView alloc] initWithFrame:CGRectMake(20,blightView.frame.size.height-20,blightView.frame.size.width-40,20)];
    blightPtogress.backgroundColor = [UIColor clearColor];
    blightPtogress.trackTintColor =[UIColor blackColor];
    blightPtogress.progressTintColor =[UIColor whiteColor];
    blightPtogress.progress =0.5f;
    // 改變進(jìn)度條的粗細(xì)
    blightPtogress.transform = CGAffineTransformMakeScale(1.0f,2.0f);
    blightPtogress.progressViewStyle=UIProgressViewStyleBar;
    [blightView addSubview:blightPtogress];
    
    volumeProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(20,blightView.frame.size.height-20,blightView.frame.size.width-40,20)];
    volumeProgress.backgroundColor = [UIColor clearColor];
    volumeProgress.trackTintColor =[UIColor blackColor];
    volumeProgress.progress =0.5f;
    volumeProgress.transform = CGAffineTransformMakeScale(1.0f,2.0f);
    volumeProgress.progressViewStyle=UIProgressViewStyleBar;
    volumeProgress.progressTintColor =[UIColor whiteColor];
    [voiceView addSubview:volumeProgress];

視圖添加UIPanGestureRecognizer手勢(shì)

// 添加滑動(dòng)手勢(shì)
    UIPanGestureRecognizer  *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDown:)];
    panGesture.delegate=self;
    [self addGestureRecognizer:panGesture];

下面是主要的手勢(shì)控制方法横堡,需要實(shí)現(xiàn)手勢(shì)代理方法UIGestureRecognizerDelegate 大體思路是:根據(jù)代理方法,取到滑動(dòng)的方向冠桃,然后在上下滑動(dòng)case中判斷是左邊的屏幕還是右邊的屏幕, 上代碼:

#pragma mark - 手勢(shì)代理 解決手勢(shì)沖突問題,不要忘記添加代理delegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch{
    
    // NSLog(@"touch.view=====%@",touch.view);
    if([touch.view isKindOfClass:[UISlider class]]){
        return NO;
    }else{
        return YES;
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    _currentPoint = [[touches anyObject] locationInView:self];
}

-(void)panGestureDown:(UIPanGestureRecognizer*)sender{
    
    if(isFullScreen==NO){
        return;// 只有橫屏才可以添加手勢(shì)
    }
    
    CGPoint point= [sender locationInView:self];// 上下控制點(diǎn)
    CGPoint tranPoint=[sender translationInView:self];//播放進(jìn)度
    typedef NS_ENUM(NSUInteger, UIPanGestureRecognizerDirection) {
        UIPanGestureRecognizerDirectionUndefined,
        UIPanGestureRecognizerDirectionUp,
        UIPanGestureRecognizerDirectionDown,
        UIPanGestureRecognizerDirectionLeft,
        UIPanGestureRecognizerDirectionRight
    };
    static UIPanGestureRecognizerDirection direction = UIPanGestureRecognizerDirectionUndefined;
    switch (sender.state) {
        case UIGestureRecognizerStateBegan: {
            _origional = videoSlider.value;// 記錄開始滑動(dòng)位置
            if (direction == UIPanGestureRecognizerDirectionUndefined) {
                CGPoint velocity = [sender velocityInView:self];
                BOOL isVerticalGesture = fabs(velocity.y) > fabs(velocity.x);
                if (isVerticalGesture) {
                    if (velocity.y > 0) {
                        direction = UIPanGestureRecognizerDirectionDown;
                    } else {
                        direction = UIPanGestureRecognizerDirectionUp;
                    }
                }
                else {
                    if (velocity.x > 0) {
                        direction = UIPanGestureRecognizerDirectionRight;
                    } else {
                        direction = UIPanGestureRecognizerDirectionLeft;
                    }
                }
            }
            break;
        }
        case UIGestureRecognizerStateChanged: {
            
            switch (direction) {
                case UIPanGestureRecognizerDirectionUp: {
                    
                    float dy = point.y - _currentPoint.y;
                    int index = (int)dy;
                    // 左側(cè) 上下改變亮度
                    if(_currentPoint.x <self.frame.size.width/2){
                        blightView.alpha =1.0f;
                        if(index >0){
                            [UIScreen mainScreen].brightness = [UIScreen mainScreen].brightness- 0.01;
                        }else{
                            [UIScreen mainScreen].brightness = [UIScreen mainScreen].brightness+ 0.01;
                        }
                        blightPtogress.progress =[UIScreen mainScreen].brightness;
                    }else{// 右側(cè)上下改變聲音
                        voiceView.alpha =1.0f;
                        if(index>0){
                            [self setVolumeDown];
                        }else{
                            [self setVolumeUp];
                        }
                        volumeProgress.progress =_systemVolume;
                    }
                    break;
                }
                case UIPanGestureRecognizerDirectionDown: {
                    
                    float dy = point.y - _currentPoint.y;
                    int index = (int)dy;
                    // 左側(cè) 上下改變亮度
                    if(_currentPoint.x <self.frame.size.width/2){
                        blightView.alpha =1.0f;
                        if(index >0){
                            [UIScreen mainScreen].brightness = [UIScreen mainScreen].brightness- 0.01;
                        }else{
                            [UIScreen mainScreen].brightness = [UIScreen mainScreen].brightness+ 0.01;
                        }
                        blightPtogress.progress =[UIScreen mainScreen].brightness;
                    }else{// 右側(cè)上下改變聲音
                        voiceView.alpha =1.0f;
                        if(index>0){
                            [self setVolumeDown];
                        }else{
                            [self setVolumeUp];
                        }
                        volumeProgress.progress =_systemVolume;
                    }
                    break;
                }
                case UIPanGestureRecognizerDirectionLeft: {
                   if(_isGes ==NO){
                        NSLog(@"Left");
                        _isGes =YES;
                        self.progressDragging = YES;
                    }
                    // 手勢(shì)滑動(dòng)控制 快進(jìn)進(jìn)度
                    if(tranPoint.x/SCREEN_HEIGHT +_origional <=0){
                        videoSlider.value=0.0f;
                    }else
                    {
                         videoSlider.value=tranPoint.x/SCREEN_HEIGHT+_origional;
                    }
                    
                    currentTimeLabel.text =[NSString stringWithFormat:@"%@/",[self timeToHumanString:(long)(videoSlider.value * mDuration)]];
                    break;
                }
                case UIPanGestureRecognizerDirectionRight: {
                  if(_isGes ==NO){
                        NSLog(@"Right");
                        _isGes = YES;
                        self.progressDragging = YES;
                    }
                    if(tranPoint.x/SCREEN_HEIGHT +_origional <=0){
                        
                        videoSlider.value=0.0f;
                    }else
                    {
                        videoSlider.value=tranPoint.x/SCREEN_HEIGHT+_origional;
                    }
                    
                    currentTimeLabel.text =[NSString stringWithFormat:@"%@/",[self timeToHumanString:(long)(videoSlider.value * mDuration)]];
                    break;
                }
                default: {
                    break;
                }
            }
            break;
        }
        case UIGestureRecognizerStateEnded: {
            _isGes =NO;
            NSLog(@"end");
            _origional = videoSlider.value;// 記錄結(jié)束滑動(dòng)位置
            direction = UIPanGestureRecognizerDirectionUndefined;
            [UIView animateWithDuration:0.5f animations:^{
                blightView.alpha =0.0f;
                voiceView.alpha=0.0f;
            }];
            break;
        }
        default:
            break;
    }
}
-(void)setVolumeUp
{
    _systemVolume = _systemVolume+0.01;
    [mMplayer setVolume:_systemVolume];
}
-(void)setVolumeDown{
    
    _systemVolume = _systemVolume-0.01;
    [mMplayer setVolume:_systemVolume];
}

具體的代碼地址:我的Github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末翅萤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子腊满,更是在濱河造成了極大的恐慌套么,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碳蛋,死亡現(xiàn)場(chǎng)離奇詭異胚泌,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)肃弟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門玷室,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人笤受,你說我怎么就攤上這事穷缤。” “怎么了箩兽?”我有些...
    開封第一講書人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵津肛,是天一觀的道長。 經(jīng)常有香客問我汗贫,道長身坐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任落包,我火速辦了婚禮部蛇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咐蝇。我一直安慰自己涯鲁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著抹腿,像睡著了一般岛请。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上幢踏,一...
    開封第一講書人閱讀 51,215評(píng)論 1 299
  • 那天髓需,我揣著相機(jī)與錄音许师,去河邊找鬼房蝉。 笑死,一個(gè)胖子當(dāng)著我的面吹牛微渠,可吹牛的內(nèi)容都是我干的搭幻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼逞盆,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼檀蹋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起云芦,我...
    開封第一講書人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤俯逾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后舅逸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體桌肴,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年琉历,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坠七。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡旗笔,死狀恐怖彪置,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蝇恶,我是刑警寧澤拳魁,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站撮弧,受9級(jí)特大地震影響的猛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜想虎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一卦尊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舌厨,春花似錦岂却、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽署浩。三九已至,卻和暖如春扫尺,著一層夾襖步出監(jiān)牢的瞬間筋栋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來泰國打工正驻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弊攘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓姑曙,卻偏偏與公主長得像襟交,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子伤靠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容