解决调节音量卡顿问题
//
// VolumeController.m
// Test
//
// Created by 王景伟 on 2018/12/21.
// Copyright © 2018 王景伟. All rights reserved.
//
#import "VolumeController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "WJMPVolumeView.h"
#import "WJSlider.h"
// 枚举值,包含水平移动方向和垂直移动方向
typedef NS_ENUM(NSInteger, PanDirection){
PanDirectionHorizontalMoved, // 横向移动
PanDirectionVerticalMoved // 纵向移动
};
@interface VolumeController ()
@property (nonatomic, strong) UISlider *mpVolumeSlider;
@property (nonatomic, strong) dispatch_source_t timer;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
/** 手势滑动方向 */
@property (nonatomic, assign) PanDirection panDirection;
@property (nonatomic, strong) UISlider *meSlider;
@end
@implementation VolumeController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
volumeView.hidden = YES;
for (UIView *view in [volumeView subviews]) {
if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
_mpVolumeSlider = (UISlider *)view; break;
}
}
[_mpVolumeSlider addTarget:self action:@selector(mpVolumeSliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:volumeView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:pan];
///> 三个判断结束条件缺一不可
self.meSlider = [[WJSlider alloc]initWithFrame:CGRectMake(100, 300, 200, 50)];
_meSlider.value = _mpVolumeSlider.value;
[_meSlider addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
[_meSlider addTarget:self action:@selector(touchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
[_meSlider addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[_meSlider addTarget:self action:@selector(touchCancel:) forControlEvents:UIControlEventTouchCancel];
[self.view addSubview:self.meSlider];
}
- (void)panGesture:(UIPanGestureRecognizer *)pan {
//获取当前页面手指触摸的点
CGPoint locationPoint = [pan locationInView:pan.view];
//速率 用来判断方向
CGPoint veloctyPoint = [pan velocityInView:pan.view];
switch (pan.state) {
case UIGestureRecognizerStateBegan:{
// 使用绝对值来判断移动的方向
CGFloat x = fabs(veloctyPoint.x);
CGFloat y = fabs(veloctyPoint.y);
if (x > y) {
self.panDirection = PanDirectionHorizontalMoved;
} else if (x < y){
self.panDirection = PanDirectionVerticalMoved;
}
self.startPoint = locationPoint;
} break;
case UIGestureRecognizerStateChanged:{
self.endPoint = locationPoint;
switch (self.panDirection) {
case PanDirectionHorizontalMoved:{
} break;
case PanDirectionVerticalMoved:{
[self startGCDTimerCompletion:^{
[self setVolumeSliderWithPoint:self.endPoint];
}];
} break;
default: break;
}
} break;
case UIGestureRecognizerStateEnded:{
[self endGCDtimer];
} break;
default: break;
}
}
- (void)startGCDTimerCompletion:(void (^)(void))completion {
if (_timer) return;
NSTimeInterval period = 0.05; //设置时间间隔 0.1秒也可以
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);
// 事件回调
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion();
});
});
// 开启定时器
dispatch_resume(_timer);
}
- (void)endGCDtimer {
if (!_timer) return;
dispatch_source_cancel(_timer);
_timer = nil;
}
- (void)setVolumeSliderWithPoint:(CGPoint)point {
///> 目前的value
CGFloat nowVolumeValue = _mpVolumeSlider.value;
CGFloat changeValue = (point.y - _startPoint.y)/ 200.0;
[self.mpVolumeSlider setValue:nowVolumeValue += changeValue animated:YES];
NSLog(@"changeValue %f",changeValue);
_meSlider.value = (nowVolumeValue += changeValue);
_startPoint = point;
}
- (void)touchDown:(UISlider *)slider {
NSLog(@"%s",__func__);
[self startGCDTimerCompletion:^{
[self.mpVolumeSlider setValue:slider.value animated:YES];
[self.mpVolumeSlider sendActionsForControlEvents:UIControlEventAllEvents];
}];
}
- (void)touchCancel:(UISlider *)slider {
NSLog(@"%s",__func__);
[self endGCDtimer];
}
- (void)touchUpOutside:(UISlider *)slider {
NSLog(@"%s",__func__);
[self endGCDtimer];
}
- (void)touchUpInside:(UISlider *)slider {
NSLog(@"%s",__func__);
[self endGCDtimer];
}
- (void)mpVolumeSliderValueChanged:(UISlider *)slider {
if (_timer) return;
_meSlider.value = _mpVolumeSlider.value;
}
@end
网友评论