代码之家  ›  专栏  ›  技术社区  ›  mahboudz

我能用这种方法检测多指敲击吗?

  •  1
  • mahboudz  · 技术社区  · 15 年前

    这是启用了多个触摸的视图的TouchsBegan方法。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
        if ([touches count] > 1)
            NSLog(@"multi touches: %d fingers", [touches count]);
    
        NSUInteger numTaps = [touch tapCount];
    
        if (numTaps == 1) {
            NSLog(@"single tap");
        } else {
            NSLog(@"multi tap: %d", numTaps);
        }
    }
    

    我似乎从来没有记录过多点触摸。只需一次和两次点击。我错了吗?我以为这和触摸计数一样容易?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Mehrdad Afshari    15 年前

    你应该设置 multipleTouchEnabled 属性到 YES 在视图上让它向您发送多个 UITouch 物体。

    除此之外,只有 乌托奇 传递已更改的对象。如果触摸某个位置而不移动手指,然后再触摸另一个位置,则只会传递新的触摸对象。您应该查询 UIEvent 视图中所有活动触摸的对象:

    [event touchesForView:self]
    
        2
  •  1
  •   mahboudz    15 年前

    我尝试了三种不同的方法,只有一种方法可以返回两到五个手指点击。获胜机制为nsset*touch=[事件所有接触];

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch* touch = [touches anyObject];
        // the next line will not ever return a multiple tap
        if ([touches count] > 1)
            NSLog(@"multi-touches %d", [touches count]);
        // the next line will return up to 5 (verified) simultaneous taps (maybe more)
            NSSet *touch2 = [event allTouches];
        if ([touch2 count] > 1)
            NSLog(@"multi-touches2 %d", [touch2 count]);
        // the next line only returns 1 tap
            NSSet *touch3 = [event touchesForView:self];
        if ([touch3 count] > 1)
            NSLog(@"multi-touches2 %d", [touch3 count]);
    }
    
        3
  •  -1
  •   Patricia    14 年前

    获胜机制?有错误和变量甚至没有被使用?

    从distinct objective-c类型传递“touchesforview:”的参数1时,不兼容的object-c类型“struct aviewcontroller*”,应为“struct uiview*”

    aviewcontroller.m:64:未使用的变量“touch”