如何获取滑动手势的位置

How to get position of swipe gesture

我正在尝试使用以下代码获取滑动的位置,但无论我在屏幕上的哪个位置滑动,它 returns 相同的位置。

代码:

-(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;
}

输出:

Tap Detected (286.500000,460.500000) 1
Left Swipe Detected (0.000000,0.000000) 1

此代码是否足以找到滑动的位置。我想知道如何在 objective-c 中找到滑动的位置。有什么我想念的吗?为什么触摸次数总是 1。当我设置 minimum touches required 超过 1 次时,没有检测到滑动。

我不知道为什么会这样,但是 tapGesture 在 window 级别获取位置,而 swipeGesture 从 View 级别获取位置。因此,在使用 swipeGesture 的情况下,位置检索功能变为 this

event = [sender locationInView: currentviewController.view ];

要获取 currentviewcontroller,我们可以创建函数来获取它并使用上面的代码,或者简单地在 UIViewController 中定义函数和 gesturerecognizer 并使用它,

event = [sender locationInView: self.view ];