UIPanGestureRecognizer上下左右滑动方向判断算法
来源:互联网 发布:教育软件市场 编辑:程序博客网 时间:2024/05/26 02:20
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
elseif (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
- UIPanGestureRecognizer上下左右滑动方向判断算法
- UIPanGestureRecognizer上下左右滑动方向判断算法
- UIPanGestureRecognizer上下左右滑动方向判断算法
- 利用UIPanGestureRecognizer 判断手势滑动方向
- 利用UIPanGestureRecognizer 判断手势滑动方向
- Unity3d 触屏手机滑动事件(上下左右方向精确判断)
- 如何拦截固定方向的拖拽(滑动/ UIPanGestureRecognizer)手势
- 判断滑动方向UITableView
- 判断scrollviewd滑动方向
- touch滑动判断方向
- 判断手指上下左右的滑动距离
- uitableview scrollview 判断滑动方向
- uitableview scrollview 判断滑动方向
- 如何判断UIScrollView滑动方向
- 判断scrollView的滑动方向
- 判断ScrollView滑动的方向
- android判断viewpager滑动方向
- 判断UISrollview的滑动方向
- Caused by: java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class
- Eclipse 3.X 显示行号的设置
- Java Web工程,常见文件的作用
- UI系列__关于LinearLayout中layout_gravity不能居中
- Ubuntu终端中字体颜色配置
- UIPanGestureRecognizer上下左右滑动方向判断算法
- 约瑟夫环(已知N个人,围坐在一张圆桌周围)
- Java中的成员方法与构造方法
- android在onCreat中获得状态栏高度
- 颜色代码:网页颜色代码大全及色彩搭配教程
- 这篇文章来自我的微信朋友圈,并不特别好玩,但可以给创业者补点财务知识
- 调查显示英国投资吸引力冠绝欧洲
- android程序开启动画封装
- Java为什么不支持多继承