我無法找到我如何獲得Xcode中iPhone上的鼠標/手指座標。如何獲取手指座標
我想:
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation];
但這似乎不是爲iPhone工作? 該API無法識別它。
有沒有人知道一種方法來做到這一點?
我無法找到我如何獲得Xcode中iPhone上的鼠標/手指座標。如何獲取手指座標
我想:
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation];
但這似乎不是爲iPhone工作? 該API無法識別它。
有沒有人知道一種方法來做到這一點?
這裏是蘋果的doc:UIResponder Class Reference。而在一個UIView
或UIViewController
的子類,你可以添加這個方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touched.view];
NSLog(@"x=%.2f y=%.2f", location.x, location.y);
}
你正在使用可可類。對於iPhone有cocoa.touch,你需要它的類UIEvent。
allTouches方法返回窗口的所有觸摸事件。一旦觸摸,您可以詢問它的位置。
例子:
void event : (UIEvent*) event
{
NSSet* touches=[event allTouches];
for(UITouch* touch in touches)
{
CGPoint point=[touch locationInView: yourView];
<Do what you need to do with the point>
}
}
我不完全理解我使用的代碼不工作,因爲它來自不同的語言?但我需要在iPhone上獲取我的手指/鼠標的x和y座標。 –
現在我發佈了一些示例代碼。您的代碼無法正常工作,因爲使用的是另一個框架,可可用於max os。可可觸摸框架適用於iOS。 –
您可以使用UITapGestureRecognizer認識到其中手指觸摸在屏幕上。爲此,您必須在控件上添加一個UITapGestureRecognizer對象/插座,您可以在其中處理觸摸,例如我在示例中添加了View上的UITapGestureRecognizer。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTappedHere:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapGesture];
self.view.userInteractionEnabled = YES;
- (void)fingerTappedHere:(UITapGestureRecognizer *)sender {
CGPoint touchedPoint = [sender locationInView:self.view];
NSLog(@"x = %f, y = %f", touchedPoint.x, touchedPoint.y);
}
希望它會幫助你。如需進一步查詢,請通過[email protected]與我們聯繫。我們會盡力解決。
TY這個完美的作品。 –