之前做了一個(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