【问题标题】:How do I access only one of my touches in my touches began event如何在我的触摸开始事件中仅访问我的一次触摸
【发布时间】:2009-12-29 18:07:15
【问题描述】:

我有:

UITouch *touch = [touches anyObject];

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

我想做的是在ifstatement 中询问它,这两个触摸是分开的。

【问题讨论】:

    标签: iphone uitouch multi-touch


    【解决方案1】:

    您可以对触摸进行迭代:

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

    【讨论】:

    • 谢谢 那是我需要做的但不明白
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多