I am trying to get the position of swipe using below code, but it returns same position no matter where I swipe on the screen.
CODE:
-(void) recognizeTapGesture : (UITapGestureRecognizer*)sender {
CGPoint event;
event = [sender locationInView: nil ];
NSLog(@"Tap Detected (%f,%f) %lu",event.x,event.y,(unsigned long)sender.numberOfTouches);
}
-(void) recognizeLeftSwipeGesture : (UISwipeGestureRecognizer*)sender {
CGPoint event;
event = [sender locationInView: nil ];
NSLog(@"Left Swipe Detected (%f,%f) %lu",event.x,event.y,(unsigned long)sender.numberOfTouches);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_TapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recognizeTapGesture:)];
_LeftswipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector(recognizeLeftSwipeGesture:) ];
_LeftswipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
_LeftswipeGesture.cancelsTouchesInView = NO;
_TapGesture.cancelsTouchesInView = NO;
[[self window] addGestureRecognizer:_LeftswipeGesture];
[[self window] addGestureRecognizer:_TapGesture];
return YES;
}
OUTPUT:
Tap Detected (286.500000,460.500000) 1
Left Swipe Detected (0.000000,0.000000) 1
Is this code sufficient, to find position of a swipe. I want to know how to find position of swipe in objective-c. Is there something I am missing ?. Why is the number of touches always 1. When I set minimum touches required more than one, swipe is not detected.
I don't know why exactly this is the case, but tapGesture gets location at window level, while swipeGesture gets location from View level. Hence in case of swipeGesture the location retrieval function becomes this
event = [sender locationInView: currentviewController.view ];
To get currentviewcontroller, we can either create function to get it and use above code or simply define the function and gesturerecognizer in UIViewController and use this,
event = [sender locationInView: self.view ];