2016-05-23 162 views
1

当用户在视图上移动手指时,我需要获取每个触摸点。iOS - 如何正确获取接触点?

这是我如何获得积分我CustomView.m

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 

    UITouch *touch = [[touches allObjects] objectAtIndex:0]; 

    CGPoint touchLocation = [touch locationInView:self]; 

    NSLog(@"touch location is %f", touchLocation.x); 
} 

然后我检查日志,输出是:

2016-05-23 19:49:45.116 AutoLayoutTest[32321:3809723] touch location is 6.000000 
2016-05-23 19:49:45.134 AutoLayoutTest[32321:3809723] touch location is 10.500000 
2016-05-23 19:49:45.151 AutoLayoutTest[32321:3809723] touch location is 17.500000 
2016-05-23 19:49:45.169 AutoLayoutTest[32321:3809723] touch location is 29.000000 
2016-05-23 19:49:45.185 AutoLayoutTest[32321:3809723] touch location is 41.500000 
2016-05-23 19:49:45.202 AutoLayoutTest[32321:3809723] touch location is 52.000000 
2016-05-23 19:49:45.220 AutoLayoutTest[32321:3809723] touch location is 62.500000 
2016-05-23 19:49:45.237 AutoLayoutTest[32321:3809723] touch location is 68.500000 
2016-05-23 19:49:45.254 AutoLayoutTest[32321:3809723] touch location is 77.000000 
2016-05-23 19:49:45.272 AutoLayoutTest[32321:3809723] touch location is 84.500000 
2016-05-23 19:49:45.288 AutoLayoutTest[32321:3809723] touch location is 90.000000 
2016-05-23 19:49:45.305 AutoLayoutTest[32321:3809723] touch location is 96.000000 
2016-05-23 19:49:45.322 AutoLayoutTest[32321:3809723] touch location is 100.500000 
2016-05-23 19:49:45.339 AutoLayoutTest[32321:3809723] touch location is 105.500000 
2016-05-23 19:49:45.357 AutoLayoutTest[32321:3809723] touch location is 109.500000 
2016-05-23 19:49:45.374 AutoLayoutTest[32321:3809723] touch location is 116.000000 
2016-05-23 19:49:45.391 AutoLayoutTest[32321:3809723] touch location is 121.000000 
2016-05-23 19:49:45.409 AutoLayoutTest[32321:3809723] touch location is 127.000000 
2016-05-23 19:49:45.426 AutoLayoutTest[32321:3809723] touch location is 132.500000 
2016-05-23 19:49:45.443 AutoLayoutTest[32321:3809723] touch location is 138.500000 
2016-05-23 19:49:45.460 AutoLayoutTest[32321:3809723] touch location is 145.000000 
2016-05-23 19:49:45.478 AutoLayoutTest[32321:3809723] touch location is 151.000000 
2016-05-23 19:49:45.495 AutoLayoutTest[32321:3809723] touch location is 156.000000 
2016-05-23 19:49:45.512 AutoLayoutTest[32321:3809723] touch location is 161.500000 
2016-05-23 19:49:45.529 AutoLayoutTest[32321:3809723] touch location is 166.500000 
2016-05-23 19:49:45.546 AutoLayoutTest[32321:3809723] touch location is 173.000000 
2016-05-23 19:49:45.563 AutoLayoutTest[32321:3809723] touch location is 178.500000 
2016-05-23 19:49:45.580 AutoLayoutTest[32321:3809723] touch location is 183.500000 
2016-05-23 19:49:45.597 AutoLayoutTest[32321:3809723] touch location is 189.000000 
2016-05-23 19:49:45.614 AutoLayoutTest[32321:3809723] touch location is 194.000000 
2016-05-23 19:49:45.632 AutoLayoutTest[32321:3809723] touch location is 199.500000 
2016-05-23 19:49:45.649 AutoLayoutTest[32321:3809723] touch location is 204.000000 
2016-05-23 19:49:45.666 AutoLayoutTest[32321:3809723] touch location is 209.500000 
2016-05-23 19:49:45.683 AutoLayoutTest[32321:3809723] touch location is 214.500000 
2016-05-23 19:49:45.700 AutoLayoutTest[32321:3809723] touch location is 220.000000 
2016-05-23 19:49:45.717 AutoLayoutTest[32321:3809723] touch location is 224.000000 
2016-05-23 19:49:45.735 AutoLayoutTest[32321:3809723] touch location is 229.000000 
2016-05-23 19:49:45.752 AutoLayoutTest[32321:3809723] touch location is 233.500000 
2016-05-23 19:49:45.769 AutoLayoutTest[32321:3809723] touch location is 237.500000 
2016-05-23 19:49:45.786 AutoLayoutTest[32321:3809723] touch location is 240.500000 
2016-05-23 19:49:45.803 AutoLayoutTest[32321:3809723] touch location is 244.000000 
2016-05-23 19:49:45.821 AutoLayoutTest[32321:3809723] touch location is 247.000000 
2016-05-23 19:49:45.838 AutoLayoutTest[32321:3809723] touch location is 249.000000 
2016-05-23 19:49:45.855 AutoLayoutTest[32321:3809723] touch location is 251.000000 
2016-05-23 19:49:45.872 AutoLayoutTest[32321:3809723] touch location is 252.000000 
2016-05-23 19:49:45.889 AutoLayoutTest[32321:3809723] touch location is 254.000000 
2016-05-23 19:49:45.907 AutoLayoutTest[32321:3809723] touch location is 255.000000 
2016-05-23 19:49:45.925 AutoLayoutTest[32321:3809723] touch location is 256.000000 
2016-05-23 19:49:45.955 AutoLayoutTest[32321:3809723] touch location is 256.500000 
2016-05-23 19:49:46.056 AutoLayoutTest[32321:3809723] touch location is 257.500000 
2016-05-23 19:49:46.073 AutoLayoutTest[32321:3809723] touch location is 258.500000 
2016-05-23 19:49:46.090 AutoLayoutTest[32321:3809723] touch location is 260.500000 
2016-05-23 19:49:46.108 AutoLayoutTest[32321:3809723] touch location is 261.500000 
2016-05-23 19:49:46.125 AutoLayoutTest[32321:3809723] touch location is 262.500000 
2016-05-23 19:49:46.142 AutoLayoutTest[32321:3809723] touch location is 263.500000 
2016-05-23 19:49:46.159 AutoLayoutTest[32321:3809723] touch location is 264.500000 
2016-05-23 19:49:46.191 AutoLayoutTest[32321:3809723] touch location is 265.000000 
2016-05-23 19:49:46.268 AutoLayoutTest[32321:3809723] touch location is 265.500000 
2016-05-23 19:49:46.293 AutoLayoutTest[32321:3809723] touch location is 266.500000 
2016-05-23 19:49:46.317 AutoLayoutTest[32321:3809723] touch location is 267.000000 
2016-05-23 19:49:46.334 AutoLayoutTest[32321:3809723] touch location is 268.000000 
2016-05-23 19:49:46.355 AutoLayoutTest[32321:3809723] touch location is 269.000000 
2016-05-23 19:49:46.387 AutoLayoutTest[32321:3809723] touch location is 269.500000 
2016-05-23 19:49:46.412 AutoLayoutTest[32321:3809723] touch location is 270.000000 
2016-05-23 19:49:46.443 AutoLayoutTest[32321:3809723] touch location is 271.000000 
2016-05-23 19:49:46.488 AutoLayoutTest[32321:3809723] touch location is 272.000000 
2016-05-23 19:49:46.544 AutoLayoutTest[32321:3809723] touch location is 272.500000 
2016-05-23 19:49:46.598 AutoLayoutTest[32321:3809723] touch location is 273.500000 
2016-05-23 19:49:46.624 AutoLayoutTest[32321:3809723] touch location is 274.000000 

敬请注意:打印的值仅仅是价值AT X轴。因为我只关心x轴的变化。

问题是为什么有一些点未检测到?我期待当用户在视图上拖动他的手指时,捕捉的点应该是6,7,8,9,10,11而不是6,10,17,29,41 ...

我很漂亮可以肯定的是,当我通过在视图上拖动手指进行测试时,我以正常的速度进行测试,而不是轻扫。所以我不认为被捕获的点应该是这样的。

我也尝试从平底手势识别器中获得积分。结果非常相似。

我做错了吗?或者有没有更好的方法来做到这一点?

+0

如果我可能会问,你想要怎么处理聚集点? – booleanBoy

+0

@booleanBoy我想在用户移动他的手指时画一条线。并且我想要改变该线的某个点的颜色,因为用户的手指通过该点。 –

+0

您是否试图画出一条直线,因为用户沿着x轴移动了他的手指? – booleanBoy

回答

1

iPad屏幕以60 Hz的频率扫描。作为触摸点坐标得到的是当手指位置发生扫描时手指的位置。扫描本身是通过每行数字化仪网格400微秒持续时间的电脉冲完成的。

随着新款iPad的频率升高至120Hz(iPad Air 2和2 mini),iPad Pro系列甚至可以240Hz扫描。您可以通过coalesced touches functionality获得这些中间点。以下是它如何在objC中完成的:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    for (UITouch *touch in touches) { 
     for (UITouch* coalescedTouch in [event coalescedTouchesForTouch:touch]) { 
      CGPoint newPoint = [coalescedTouch locationInView:self]; 
     } 
    } 
} 

如果手指移动速度足够快,即使这样您的坐标也会有空隙。插入以获取手指位于中间位置的时间 - 屏幕不会提供更高的精度。

2

这是关于精度。你不能做任何事情,因为用户快速移动他的手指以获得每个像素的价值。

+0

嗯..如果是这样的话。忘记积分的准确性。当他的手指触摸它时,我是否有可能检测到用户触摸到看起来像点的视图? –

+0

是的,您必须使用函数CGRectIntersectsRect来检测手指是否触及点视图 –

+0

好的。谢谢,我会尝试。 –