Question

I would like to detect when a user makes a swipe gesture, and detect it's direction (left, right, up or down). I need to detect it not when the gesture is finished, but just when the iPhone knows the direction, even if it's not finished.

I am using XCode 5, and designing for iPhone 5 with iOS 7.

I would like to know the code that I have to paste to the .h, and to the .m, and if I have to drag and drop any item to the mainView.

Was it helpful?

Solution

If you want to know the direction when the gesture is not finished you would probably need the UIPanGestureRecognizer and detect the direction using the velocityInView: method.

in the *.h file:

@interface ViewController : UIViewController

@property (nonatomic, strong) UIPanGestureRecognizer *recognizer;

@end

in the *.m file:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

    [self.view addGestureRecognizer:self.recognizer];
}

-(void)panGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Velocity: %@", NSStringFromCGPoint([sender velocityInView:self.view]));

    // Here You need to determine the direction
}
@end

OTHER TIPS

From Docs:

Gestures are either discrete or continuous. A discrete gesture, such as a tap, occurs once. A continuous gesture, such as pinching, takes place over a period of time. For discrete gestures, a gesture recognizer sends its target a single action message. A gesture recognizer for continuous gestures keeps sending action messages to its target until the multitouch sequence ends,

So if you need to "detect it not when the gesture is finished, but just when the iphone knows the direction, even if it's not finished" you could not use UISwipeGestureRecognizer since it is a discrete one. You should use UIPanGestureRecognizer and analyse it is results to check is there a swipe gesture or not.

To have a good example how it works, you can download a sample code provided by Apple:

https://developer.apple.com/library/ios/samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

Hope that helps ;)

You can easily get using UISwipeGestureRecognizers property directions... After :----

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.yourView addGestureRecognizer:swipeRight];
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.gifWebView addGestureRecognizer:swipeLeft];

-(void)swipeRight{
//Do whatever you want
}
-(void)swipeLeft{
//Do whatever you want
}

And For Before Swipe you can use UIPanGestureRecognizer

I hope it help you

Swipe gestures are very quick. Swipe gesture recognizers are designed to fire once, not continuously. I don't think you will be able to get information about a swipe gesture that is "in flight" very easily.

If this is urgent, you will probably have to create a custom subclass of swipe gesture recognizer and add additional logic and delegate messages that would notify the delegate once the gesture recognizer decides that it has detected a swipe. That is way beyond your current skill level however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top