播放器上下滑动调节音量,左右滑动调节进度的实现方案
2016-06-28 本文已影响742人
小黑丶
在实现播放器的过程中,可能需要在滑动屏幕时要实现左右滑动快进快退,上下滑动调节音量的需求,我的实现方案是只添加一个<code>
UIPanGestureRecognizer</code>进行实现.
下面说说解决方案吧
1 添加手势
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self addGestureRecognizer:recognizer];
2.判断滑动的方向
2.1 添加一个最小变化
添加一个<code>gestureMinimumTranslation</code>用于判断方向
<code>
CGFloat const gestureMinimumTranslation = 5.0;</code>
2.2定义一个枚举变量,标识方向
typedef enum : NSInteger{
PanDirectionNone,
PanDirectionUp,
PanDirectionDown,
PanDirectionRight,
PanDirectionLeft
} PanDirection;
@property (nonatomic, assign) PanDirection direction;
2.3 定义方法,判断滑动的方向
// This method will determine whether the direction of the user's swipe
- (PanDirection)determinePanDirectionIfNeeded:(CGPoint)translation
{
if (self.direction != PanDirectionNone)
return self.direction;
if (fabs(translation.x) > gestureMinimumTranslation)
{
BOOL gestureHorizontal = NO;
if (translation.y ==0.0)
gestureHorizontal = YES;
else
gestureHorizontal = (fabs(translation.x / translation.y) >5.0);
if (gestureHorizontal)
{
if (translation.x >0.0)
return PanDirectionRight;
else
return PanDirectionLeft;
}
}
else if (fabs(translation.y) > gestureMinimumTranslation)
{
BOOL gestureVertical = NO;
if (translation.x ==0.0)
gestureVertical = YES;
else
gestureVertical = (fabs(translation.y / translation.x) >5.0);
if (gestureVertical)
{
if (translation.y >0.0)
return PanDirectionDown;
else
return PanDirectionUp;
}
}
return self.direction;
}
2.3实现滑动方法
- (void)handleSwipe:(UIPanGestureRecognizer *)gesture
{
CGFloat widthSeconds = 90;
CGFloat percentLength = widthSeconds / self.view.frame.size.width;
CGFloat panDirectionY = [gesture velocityInView:gesture.view].y;
CGPoint translation = [gesture translationInView:gesture.view];
if (gesture.state ==UIGestureRecognizerStateBegan)
{
self.direction = PanDirectionNone;
self.startPoint = [gesture locationInView:gesture.view];
}
if (gesture.state == UIGestureRecognizerStateChanged )
{
self.direction = [self determinePanDirectionIfNeeded:translation];
switch (self.direction) {
case PanDirectionUp:
case PanDirectionDown:
{
self.isChangingVolume = YES;
//
if (panDirectionY > 0) {
MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
musicController.volume -= 0.01;
}
else{
MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
musicController.volume += 0.01;
}
break;
}
case PanDirectionRight: // 向右划(快进)
{
// 此时可以根据滑动的时间,在界面上进行显示快进,快退时间
CGPoint endPoint = [gesture locationInView:gesture.view];
int panLength = endPoint.x - self.startPoint.x;
if (panLength > 0) {
// 计算滑动的距离显示
NSInteger timeLength = panLength * percentLength;
NSLog(@"%zd",timeLength);
}else{
// // 向右划的过程中,向左划,而且比初始位置还要向左
CGPoint endPoint = [gesture locationInView:gesture.view];
int panLength = endPoint.x - self.startPoint.x;
int timeLength = panLength * percentLength * -1;
NSLog(@"%zd",timeLength);
}
break;
}
case PanDirectionLeft: // 向左划 快退手势
{
// 此时可以根据滑动的时间,在界面上进行显示快进,快退时间
CGPoint endPoint = [gesture locationInView:gesture.view];
int panLength = endPoint.x - self.startPoint.x;
if (panLength < 0) {
int timeLength = panLength * percentLength * -1;
NSLog(@"%zd",timeLength);
}else{ // 向左划的过程中,向右划,而且比初始位置还要向右
CGPoint endPoint = [gesture locationInView:gesture.view];
int panLength = endPoint.x - self.startPoint.x;
int timeLength = panLength * percentLength;
NSLog(@"%zd",timeLength);
}
}
default:
break;
}
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
if (!self.isChangingVolume) { // 如果不是改变声音
CGPoint endPoint = [gesture locationInView:gesture.view];
CGFloat panLength = endPoint.x - self.startPoint.x;
NSLog(@"%f",panLength);
CGFloat seconds = panLength * percentLength ;
// 此时计算出快进/快退的时间,可以让播放器实现快进快退
}
self.isChangingVolume = NO;
}
}