UIPanGestureRecognizer上下左右滑動方向判斷演算法
CGFloat const gestureMinimumTranslation = 20.0;
typedef enum :NSInteger {
kCameraMoveDirectionNone,
kCameraMoveDirectionUp,
kCameraMoveDirectionDown,
kCameraMoveDirectionRight,
kCameraMoveDirectionLeft
} CameraMoveDirection;
@interfaceViewController ()
{
CameraMoveDirection direction;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self.viewWithGestureRecognizer addGestureRecognizer:recognizer];
}
// This is my gesture recognizer handler, which detects movement in a particular
// direction, conceptually tells a camera to start moving in that direction
// and when the user lifts their finger off the screen, tells the camera to stop.
- (void)handleSwipe:(UIPanGestureRecognizer *)gesture
{
CGPoint translation = [gesture translationInView:self.view];
if (gesture.state ==UIGestureRecognizerStateBegan)
{
direction = kCameraMoveDirectionNone;
}
else if (gesture.state == UIGestureRecognizerStateChanged && direction == kCameraMoveDirectionNone)
{
direction = [self determineCameraDirectionIfNeeded:translation];
// ok, now initiate movement in the direction indicated by the user's gesture
switch (direction) {
case kCameraMoveDirectionDown:
NSLog(@"Start moving down");
break;
case kCameraMoveDirectionUp:
NSLog(@"Start moving up");
break;
case kCameraMoveDirectionRight:
NSLog(@"Start moving right");
break;
case kCameraMoveDirectionLeft:
NSLog(@"Start moving left");
break;
default:
break;
}
}
elseif (gesture.state == UIGestureRecognizerStateEnded)
{
// now tell the camera to stop
NSLog(@"Stop");
}
}
// This method will determine whether the direction of the user's swipe
- (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation
{
if (direction != kCameraMoveDirectionNone)
return direction;
// determine if horizontal swipe only if you meet some minimum velocity
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 kCameraMoveDirectionRight;
else
return kCameraMoveDirectionLeft;
}
}
// determine if vertical swipe only if you meet some minimum velocity
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 kCameraMoveDirectionDown;
else
return kCameraMoveDirectionUp;
}
}
return direction;
}
@end
相關文章
- H5觸控事件判斷滑動方向H5事件
- 判斷滾動條是否滑動到底部
- jQuery滑動方式上下左右滾動效果jQuery
- UIPanGestureRecognizer進行檢視滑動並處理手勢衝突UI
- React 移動端判斷使用者劃屏方向React
- Python-OpenCV 處理視訊(五): 運動方向判斷PythonOpenCV
- 帶貨直播原始碼,確定ViewPager滑塊滑動方向原始碼Viewpager
- C語言判斷素數,判斷質素演算法C語言演算法
- 用Activity的onTouchEvent方法實現監聽手指上下左右滑動
- 聊天平臺原始碼,簡單使用 禁止滑動和設定滑動方向原始碼
- 扔掉“巨無霸”指數 用iPhone來判斷美元方向iPhone
- 判斷oracle是否啟動Oracle
- 移動端的判斷
- 判斷滑鼠指標移入移出的方向程式碼例項指標
- 短視訊程式碼,ViewPager滑動方向改變Viewpager
- 演算法之複雜度判斷演算法複雜度
- python實現基於八方向判斷的斷裂連線Python
- Python-OpenCV 處理視訊(三)(四)(五): 標記運動軌跡 運動檢測 運動方向判斷PythonOpenCV
- 微軟AR眼鏡能判斷眼球方向 滑鼠從此可以退役了微軟
- 判斷移動裝置型別型別
- 判斷滾動條是否到底部
- 演算法題:判斷括號字串是否有效演算法字串
- 怎麼判斷dns汙染,怎麼判斷dns汙染,具體判斷方法DNS
- JS的判斷語句:判斷、迴圈JS
- 字元判斷字元
- Sku 多維屬性狀態判斷演算法演算法
- js函式中的if判斷和a==b判斷JS函式
- 使用帶型別判斷的比較判斷型別
- Swift 幾種動態判斷類的方法Swift
- 如何判斷滾動條到達最低端
- jquery智慧彈出層,自動判斷位置jQuery
- js資料型別判斷和陣列判斷JS資料型別陣列
- 漫畫演算法:如何判斷連結串列有環?演算法
- Python演算法實現質數(素數)判斷Python演算法
- 原型判斷方法原型
- nginx 多if判斷。Nginx
- 判斷 iOS 版本iOS
- sqlldr增加判斷。SQL