現(xiàn)在App視頻之中常用到手勢(shì)滑動(dòng)來控制音量及屏幕亮度,簡(jiǎn)單實(shí)現(xiàn)下
既然是滑動(dòng)控制,那么需要知道滑動(dòng)的方向,一般左邊上下滑動(dòng)為亮度,右邊上下滑動(dòng)為音量,左右滑動(dòng)為快進(jìn)快推。
在最開始的時(shí)候先導(dǎo)入頭文件,才能使用MPVolumeView
#import<MediaPlayer/MediaPlayer.h>
首先來定義一個(gè)枚舉來判斷滑動(dòng)方向
// 枚舉值,包含水平移動(dòng)方向和垂直移動(dòng)方向
typedef NS_ENUM(NSInteger, PanDirection){
PanDirectionHorizontalMoved, // 橫向移動(dòng)
PanDirectionVerticalMoved? ? // 縱向移動(dòng)
};
在屬性這邊
@property (nonatomic,strong) UIPanGestureRecognizer* panGes;? ? ? ? ? ? //控制音量及亮度手勢(shì)
@property (nonatomic, assign) PanDirection panDirection;? ? ? //定義枚舉變量
@property (nonatomic, strong) UISlider *volumeViewSlider;? ? ? //調(diào)節(jié)滑竿
@property (nonatomic, assign) BOOL isVolume;? ? ? ? ? ? ? ? ? ? //是否在調(diào)節(jié)音量
定義完成后,先去獲取系統(tǒng)音量,當(dāng)然順便把耳機(jī)的插拔狀態(tài)寫進(jìn)去,該方法在初始化播放器的時(shí)候直接self調(diào)用
/**
*? 獲取系統(tǒng)音量
*/
- (void)setLightAndVolume
{
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
_volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
_volumeViewSlider = (UISlider *)view;
break;
}
}
// 監(jiān)聽耳機(jī)插入和拔掉通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mic:) name:AVAudioSessionRouteChangeNotification object:nil];
}
/**
*? 耳機(jī)插入、拔出事件
*/
- (void)mic:(NSNotification*)notify
{
NSDictionary *dic = notify.userInfo;
NSInteger isEarPhone = [[dic valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (isEarPhone) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
// 耳機(jī)插入
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
{
// 拔掉耳機(jī)繼續(xù)播放
[self StartPlay];
}
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
break;
}
}
之后是對(duì)于手勢(shì)的添加,在視頻未全屏播放的情況下,不需要使用手勢(shì)去控制,所以在點(diǎn)擊全屏的時(shí)候去添加手勢(shì)(全屏按鈕點(diǎn)擊事件),在返回豎屏的時(shí)候釋放
-(void)fullScreenOrShrinkScreen:(UIButton*)sender
{
sender.selected = !sender.selected;
[self addControlVolmeAndLight:sender.selected];
if (_mjPlayerViewDelegate&&[_mjPlayerViewDelegate respondsToSelector:@selector(fullScreenOrShrinkScreenDelegate:)]) {
[_mjPlayerViewDelegate fullScreenOrShrinkScreenDelegate:sender];
}
}
#pragma Mark -- 添加手勢(shì)控制聲音及亮度
-(void)addControlVolmeAndLight:(BOOL)isLandscape
{
if (isLandscape) {
_panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(controlVolmeAndLight:)];
[self addGestureRecognizer:_panGes];
}else
{
[self removeGestureRecognizer:_panGes];
}
}
在添加完手勢(shì)后,就是對(duì)手勢(shì)進(jìn)行處理,響應(yīng)該手勢(shì)的事件,具體注釋在代碼中
#pragma Mark----滑動(dòng)手勢(shì)響應(yīng)事件
-(void)controlVolmeAndLight:(UIPanGestureRecognizer*)ges
{
//根據(jù)位置,確定是調(diào)音量還是亮度
CGPoint locationPoint = [ges locationInView:self];
// 獲取滑動(dòng)速度
CGPoint speed = [ges velocityInView:self];
// 判斷是垂直移動(dòng)還是水平移動(dòng)
switch (ges.state) {
case UIGestureRecognizerStateBegan:{ // 開始移動(dòng)
// 使用絕對(duì)值來判斷移動(dòng)的方向
CGFloat x = fabs(speed.x);
CGFloat y = fabs(speed.y);
if (x < y){ // 垂直移動(dòng)
self.panDirection = PanDirectionVerticalMoved;
// 開始滑動(dòng)的時(shí)候,狀態(tài)改為正在控制音量
if (locationPoint.x > self.bounds.size.width / 2) {
self.isVolume = YES;
}else { // 狀態(tài)改為顯示亮度調(diào)節(jié)
self.isVolume = NO;
}
}
break;
}
case UIGestureRecognizerStateChanged:{ // 正在移動(dòng)
switch (self.panDirection) {
case PanDirectionHorizontalMoved:{
//[self horizontalMoved:veloctyPoint.x]; // 水平移動(dòng)的方法只要x方向的值
break;
}
case PanDirectionVerticalMoved:{
[self verticalMoved:speed.y]; // 垂直移動(dòng)方法只要y方向的值
break;
}
default:
break;
}
break;
}
case UIGestureRecognizerStateEnded:{ // 移動(dòng)停止
// 移動(dòng)結(jié)束也需要判斷垂直或者平移
// 比如水平移動(dòng)結(jié)束時(shí),要快進(jìn)到指定位置,如果這里沒有判斷,當(dāng)我們調(diào)節(jié)音量完之后,會(huì)出現(xiàn)屏幕跳動(dòng)的bug
switch (self.panDirection) {
case PanDirectionHorizontalMoved:{
break;
}
case PanDirectionVerticalMoved:{
// 垂直移動(dòng)結(jié)束后,把狀態(tài)改為不再控制音量
self.isVolume = NO;
break;
}
default:
break;
}
break;
}
default:
break;
}
}
最后一步是調(diào)節(jié)方法(PS:亮度調(diào)節(jié)功能已有,但是它的view要自定義,還沒加上)
- (void)verticalMoved:(CGFloat)value{//該value為手指的滑動(dòng)速度,一般最快速度值不會(huì)超過10000,保證在0-1之間,往下滑為正,往上滑為負(fù) 所以用 “-=”NSLog(@"%f",value);if(self.isVolume) {self.volumeViewSlider.value-= value /10000;? ? }else{? ? ? ? ([UIScreenmainScreen].brightness-= value /10000);? ? }}
文/Sign_in_with_yo(簡(jiǎn)書作者)
原文鏈接:http://www.itdecent.cn/p/738211b94282
著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書作者”。