UIPanGestureRecognizer

UIPanGestureRecognizer

.h文件

CGFloat const gestureMinimumTranslation = 20.0 ;
typedef enum : NSInteger {
kCameraMoveDirectionNone,
kCameraMoveDirectionUp,
kCameraMoveDirectionDown,
kCameraMoveDirectionRight,
kCameraMoveDirectionLeft
} CameraMoveDirection ; @interface FCDetailViewController : FCMainViewController
{
CameraMoveDirection direction;
} .m文件 -------- - (void)viewDidLoad
{
[super viewDidLoad]; //监听滑动手势
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:recognizer];
} - (void)handleSwipeFrom:(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]; 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 ;
} }else if(gesture.state == UIGestureRecognizerStateEnded){
//NSLog (@ "Stop" );
}
} - (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation
{
if (direction != kCameraMoveDirectionNone)
return 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 kCameraMoveDirectionRight;
else
return kCameraMoveDirectionLeft;
} }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;
}
05-22 08:10