在實(shí)現(xiàn)播放器的過程中,可能需要在滑動(dòng)屏幕時(shí)要實(shí)現(xiàn)左右滑動(dòng)快進(jìn)快退,上下滑動(dòng)調(diào)節(jié)音量的需求,我的實(shí)現(xiàn)方案是只添加一個(gè)<code>
UIPanGestureRecognizer</code>進(jìn)行實(shí)現(xiàn).
下面說說解決方案吧
1 添加手勢
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self addGestureRecognizer:recognizer];
2.判斷滑動(dòng)的方向
2.1 添加一個(gè)最小變化
添加一個(gè)<code>gestureMinimumTranslation</code>用于判斷方向
<code>
CGFloat const gestureMinimumTranslation = 5.0;</code>
2.2定義一個(gè)枚舉變量,標(biāo)識(shí)方向
typedef enum : NSInteger{
PanDirectionNone,
PanDirectionUp,
PanDirectionDown,
PanDirectionRight,
PanDirectionLeft
} PanDirection;
@property (nonatomic, assign) PanDirection direction;
2.3 定義方法,判斷滑動(dòng)的方向
// 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實(shí)現(xiàn)滑動(dòng)方法
- (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: // 向右劃(快進(jìn))
{
// 此時(shí)可以根據(jù)滑動(dòng)的時(shí)間,在界面上進(jìn)行顯示快進(jìn),快退時(shí)間
CGPoint endPoint = [gesture locationInView:gesture.view];
int panLength = endPoint.x - self.startPoint.x;
if (panLength > 0) {
// 計(jì)算滑動(dòng)的距離顯示
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: // 向左劃 快退手勢
{
// 此時(shí)可以根據(jù)滑動(dòng)的時(shí)間,在界面上進(jìn)行顯示快進(jìn),快退時(shí)間
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 ;
// 此時(shí)計(jì)算出快進(jìn)/快退的時(shí)間,可以讓播放器實(shí)現(xiàn)快進(jìn)快退
}
self.isChangingVolume = NO;
}
}