我正在尝试使用下面的代码获取滑动的位置,但无论我在屏幕上滑动的位置,它都会返回相同的位置。
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;
}
输出:
Tap Detected (286.500000,460.500000) 1
Left Swipe Detected (0.000000,0.000000) 1
此代码是否足够,以找到滑动的位置。我想知道如何在objective-c中找到滑动的位置。有什么我想念的吗?为什么触摸次数始终为1.当我设置所需的最小触摸次数时,不会检测到滑动。
答案 0 :(得分:0)
我不知道为什么会出现这种情况,但tapGesture会在窗口级获取位置,而swipeGesture会从View级别获取位置。因此,在swipeGesture的情况下,位置检索功能变为此
event = [sender locationInView: currentviewController.view ];
要获取currentviewcontroller,我们可以创建函数来获取它并使用上面的代码,或者只是在UIViewController中定义函数和gesturerecognizer并使用它,
event = [sender locationInView: self.view ];