之前做了一个关于视频播放的项目,需求是 当屏幕横屏时, 上下滑动屏幕左可以改变屏幕亮度,上下滑动屏幕右侧可以改变屏幕音量。具体的实现如下:
首先 自定义亮度和音量展示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;
// 改变进度条的粗细
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
手势
// 添加滑动手势
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDown:)];
panGesture.delegate=self;
[self addGestureRecognizer:panGesture];
下面是主要的手势控制方法,需要实现手势代理方法UIGestureRecognizerDelegate
大体思路是:根据代理方法,取到滑动的方向,然后在上下
滑动case
中判断是左边的屏幕还是右边的屏幕, 上代码:
#pragma mark - 手势代理 解决手势冲突问题,不要忘记添加代理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;// 只有横屏才可以添加手势
}
CGPoint point= [sender locationInView:self];// 上下控制点
CGPoint tranPoint=[sender translationInView:self];//播放进度
typedef NS_ENUM(NSUInteger, UIPanGestureRecognizerDirection) {
UIPanGestureRecognizerDirectionUndefined,
UIPanGestureRecognizerDirectionUp,
UIPanGestureRecognizerDirectionDown,
UIPanGestureRecognizerDirectionLeft,
UIPanGestureRecognizerDirectionRight
};
static UIPanGestureRecognizerDirection direction = UIPanGestureRecognizerDirectionUndefined;
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
_origional = videoSlider.value;// 记录开始滑动位置
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;
// 左侧 上下改变亮度
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{// 右侧上下改变声音
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;
// 左侧 上下改变亮度
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{// 右侧上下改变声音
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;
}
// 手势滑动控制 快进进度
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;// 记录结束滑动位置
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