1、AVPlayerLayer所在的控件添加一个UIPanGestureRecognizer手势
2、添加一个存储上一次拖拽位置的全局变量
///手指拖动上一次的值
///手指拖动上一次的X值
@property(nonatomic,assign)CGFloat preX;
///手指拖动上一次的Y值
@property(nonatomic,assign)CGFloat preY;
3、拖拽手势对应的代码实现
///拖拽手势
- (IBAction)panLeftAndRight:(UIPanGestureRecognizer *)sender {
NSTimeInterval time = CMTimeGetSeconds(self.player.currentItem.currentTime);
NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
if (sender.state == UIGestureRecognizerStateBegan) {
[self.player pause];
if (!self.isShowToolView) {
[self removeShowTimer];
[self tapClick:nil];
}
}else if (sender.state == UIGestureRecognizerStateChanged){
CGPoint point = [sender translationInView:sender.view];
CGPoint localPoint = [sender locationInView:sender.view];
// NSLog(@"--->>%@",NSStringFromCGPoint(point));
if (fabs(point.x) >= fabs(point.y)) {//移动的时候如果x坐标的差值大于y坐标,处理快进快退
CGFloat ratio = (point.x - self.preX) / self.imageView.bounds.size.width;
if (time <= 0) return;
time += ratio * duration;
[self swipeIsRight:time];
}else{
CGFloat lightRatio = (point.y - self.preY) / self.imageView.bounds.size.height;
if (localPoint.x > self.imageView.bounds.size.width * 0.5) {//如果触摸点在屏幕的右边调整音量
// NSLog(@"移动的时候如果x坐标的差值小于y坐标,并且在屏幕的右边--%lf",localPoint.x);
MPMusicPlayerController *mpc = [MPMusicPlayerController applicationMusicPlayer];
mpc.volume -= lightRatio;
}else{//如果触摸点在屏幕的左边调整亮度
// NSLog(@"移动的时候如果x坐标的差值小于y坐标,并且在屏幕的左边");
//获取亮度
CGFloat brightess = [[UIScreen mainScreen] brightness];
brightess -= lightRatio;
//设置亮度
[[UIScreen mainScreen] setBrightness:brightess];
}
}
self.preX = point.x;
self.preY = point.y;
}else if (sender.state == UIGestureRecognizerStateEnded){
[self.player play];
self.preX = 0;
self.preY = 0;
if (self.isShowToolView) {
[self removeShowTimer];
[self addShowTimer];
}
}
}
///左右滑动快进后退
-(void)swipeIsRight:(NSTimeInterval)time
{
//更新播放器的当前时间
[self.player seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
//更新进度条和展示时间
[self updateUIConfig];
}
4、效果图
5、demo地址:https://gitee.com/flynn0129/avplayer-video-playback
网友评论