如何在我的触摸开始事件中仅访问我的触摸之一

发布于 2024-08-17 01:03:55 字数 241 浏览 5 评论 0原文

我有:

UITouch *touch = [touches anyObject];

    if ([touches count] == 2) {
        //preforming actions                                                             
    }

我想要做的是在 if 语句内询问它,其中两次触摸是分开的。

I have:

UITouch *touch = [touches anyObject];

    if ([touches count] == 2) {
        //preforming actions                                                             
    }

What I want to do Is ask it inside of the ifstatement where the two touches are seperatly.

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

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

发布评论

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

评论(2

壹場煙雨 2024-08-24 01:03:55

您可以迭代触摸:

if([touches count] == 2) {
    for(UITouch *aTouch in touches) {
        // Do something with each individual touch (e.g. find its location)
    }
}

编辑:如果您想要找到两个触摸之间的距离,并且您知道恰好有两个触摸,您可以分别抓取每个触摸,然后进行一些数学运算。例子:

float distance;
if([touches count] == 2) {
    // Order touches so they're accessible separately
    NSMutableArray *touchesArray = [[[NSMutableArray alloc] 
                                     initWithCapacity:2] autorelease];
    for(UITouch *aTouch in touches) {
        [touchesArray addObject:aTouch];
    }
    UITouch *firstTouch = [touchesArray objectAtIndex:0];
    UITouch *secondTouch = [touchesArray objectAtIndex:1];

    // Do math
    CGPoint firstPoint = [firstTouch locationInView:[firstTouch view]];
    CGPoint secondPoint = [secondTouch locationInView:[secondTouch view]];
    distance = sqrtf((firstPoint.x - secondPoint.x) * 
                     (firstPoint.x - secondPoint.x) + 
                     (firstPoint.y - secondPoint.y) * 
                     (firstPoint.y - secondPoint.y));
}

You can iterate over the touches:

if([touches count] == 2) {
    for(UITouch *aTouch in touches) {
        // Do something with each individual touch (e.g. find its location)
    }
}

Edit: if you want to, say, find the distance between the two touches, and you know there are exactly two, you can grab each separately then do some math. Example:

float distance;
if([touches count] == 2) {
    // Order touches so they're accessible separately
    NSMutableArray *touchesArray = [[[NSMutableArray alloc] 
                                     initWithCapacity:2] autorelease];
    for(UITouch *aTouch in touches) {
        [touchesArray addObject:aTouch];
    }
    UITouch *firstTouch = [touchesArray objectAtIndex:0];
    UITouch *secondTouch = [touchesArray objectAtIndex:1];

    // Do math
    CGPoint firstPoint = [firstTouch locationInView:[firstTouch view]];
    CGPoint secondPoint = [secondTouch locationInView:[secondTouch view]];
    distance = sqrtf((firstPoint.x - secondPoint.x) * 
                     (firstPoint.x - secondPoint.x) + 
                     (firstPoint.y - secondPoint.y) * 
                     (firstPoint.y - secondPoint.y));
}
初见你 2024-08-24 01:03:55

Touches 已经是一个数组了。无需将它们复制到另一个数组中 - 只需使用 [touches objectAtIndex:n] 即可访问 touch n。

Touches is already an array. There's no need to copy them into another array -- just use [touches objectAtIndex:n] to access touch n.

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