如何从 UIGesture 获取多点触摸的多个坐标

发布于 2024-11-19 04:49:13 字数 520 浏览 4 评论 0原文

我有三个手势:2 指点击、3 指点击和4 指点击。我需要相应地获取坐标。

我已尝试以下方法来协调 2 个手指点击,但应用程序不断崩溃:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSArray *twoTouch = [touches allObjects];
UITouch *tOne = [twoTouch objectAtIndex:0];
UITouch *tTwo = [twoTouch objectAtIndex:1];
CGPoint firstTouch = [tOne locationInView:[tOne view]];
CGPoint secondTouch = [tTwo locationInView:[tTwo view]];

NSLog(@"point one: %@", firstTouch);
NSLog(@"point two: %@", secondTouch);


[twoTouch release];

}

I have three gestures: 2-finger tap, 3-finger tap, and 4-finger tap. I need to get coordinates accordingly.

I have tried the following to get the coordinated of 2 fingers tap but app keeps crashing:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSArray *twoTouch = [touches allObjects];
UITouch *tOne = [twoTouch objectAtIndex:0];
UITouch *tTwo = [twoTouch objectAtIndex:1];
CGPoint firstTouch = [tOne locationInView:[tOne view]];
CGPoint secondTouch = [tTwo locationInView:[tTwo view]];

NSLog(@"point one: %@", firstTouch);
NSLog(@"point two: %@", secondTouch);


[twoTouch release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-11-26 04:49:13

首先,您的应用程序不会检查是否确实两次触摸。
如果您用一根手指点击屏幕,您将在“触摸”中获得一次触摸。

尝试这样的事情。

if(touches.count > 1 && touches.count < 3)
{
    // Your code for two touches.
}

否则,程序崩溃的部分是 [twoTouch objectAtIndex:1],因为 objectAtIndex:1 不存在。

(我知道这是一个非常老的问题,但我还是回答了。)

First of all, your application is not checking if there actually are two touches.
If you tap the screen with one finger you will get one touch in the "touches".

Try something like this.

if(touches.count > 1 && touches.count < 3)
{
    // Your code for two touches.
}

Otherwise, the part where your program crashes is the [twoTouch objectAtIndex:1] because objectAtIndex:1 doesn't exist.

(I know this is a really old question but I answered it anyways.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文