多点触控追踪问题

我正在使用多点触控书写,所以基本上我正在做的是,我手写支持,因为通常,它的用户权限,我跟着这个链接如何忽略多点触控顺序中的某些UITouch点

所以,我正在做的是,我正在touchesBegan中跟踪一个touch对象,并且只在touchesMoved中使用它。一切工作正常,但是在写一些时候,我得到了这一行

在这里输入图像说明

在上图中,您可以看到用手触摸屏幕时突然出现的粗线

这是代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touches began"); UITouch* topmostTouch = self.trackingTouch; for (UITouch *touch in touches) { bufIdx = 0; isFirstTouchPoint = YES; ctr = 0; pts[0] = [touch locationInView:self]; if(!topmostTouch || [topmostTouch locationInView:self].y > pts[0].y) { topmostTouch = touch; //touchStartPoint1 = pts[0]; } else { pts[0] = pts[3]; } } if (self.trackingTouch != nil && self.trackingTouch != topmostTouch) { [self discardDrawing]; } self.trackingTouch = topmostTouch; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // Find the touch that we track for drawing CGPoint p = [self.trackingTouch locationInView:self]; ctr++; pts[ctr] = p; if (ctr == 4) { pts[3] = midPoint(pts[2], pts[4]); for ( int i = 0; i < 4; i++) { pointsBuffer[bufIdx + i] = pts[i]; } bufIdx += 4; dispatch_async(drawingQueue, ^{ //UIBezierPath *offsetPath = [UIBezierPath bezierPath]; // ................. (2) self.currentPath = [[DrawingPath alloc] init]; [self.currentPath setPathColor:self.lineColor]; if (bufIdx == 0) return; LineSegment ls[4]; for ( int i = 0; i < bufIdx; i += 4) { if (isFirstTouchPoint) // ................. (3) { ls[0] = (LineSegment){pointsBuffer[0], pointsBuffer[0]}; [self.currentPath.path moveToPoint:ls[0].firstPoint]; isFirstTouchPoint = NO; } else { ls[0] = lastSegmentOfPrev; } float frac1 = self.lineWidth/clamp(len_sq(pointsBuffer[i], pointsBuffer[i+1]), LOWER, UPPER); // ................. (4) float frac2 = self.lineWidth/clamp(len_sq(pointsBuffer[i+1], pointsBuffer[i+2]), LOWER, UPPER); float frac3 = self.lineWidth/clamp(len_sq(pointsBuffer[i+2], pointsBuffer[i+3]), LOWER, UPPER); ls[1] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i], pointsBuffer[i+1]} ofRelativeLength:frac1]; // ................. (5) ls[2] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i+1], pointsBuffer[i+2]} ofRelativeLength:frac2]; ls[3] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i+2], pointsBuffer[i+3]} ofRelativeLength:frac3]; [self.currentPath.path moveToPoint:ls[0].firstPoint]; // ................. (6) [self.currentPath.path addCurveToPoint:ls[3].firstPoint controlPoint1:ls[1].firstPoint controlPoint2:ls[2].firstPoint]; [self.currentPath.path addLineToPoint:ls[3].secondPoint]; [self.currentPath.path addCurveToPoint:ls[0].secondPoint controlPoint1:ls[2].secondPoint controlPoint2:ls[1].secondPoint]; [self.currentPath.path closePath]; lastSegmentOfPrev = ls[3]; // ................. (7) } dispatch_async(dispatch_get_main_queue(), ^{ bufIdx = 0; [m_pathArray addObject:self.currentPath]; [self setNeedsDisplay]; }); }); pts[0] = pts[3]; pts[1] = pts[4]; ctr = 1; } } } 

所以朋友们,请帮帮我。

你将需要debugging来确认,但它似乎与touchesBegan:withEvent:的逻辑有关touchesBegan:withEvent: 在这种方法中,每检查一次新触摸以确定它是否是“最高”,但是当最高触摸确实发生变化时,您似乎不会中止/重置任何现有的绘图。

您最好的方法可能是使用日志logging来确定是否/何时“最高”的触摸正在改变以及对正在进行的行有什么影响。

另外,在touchesMoved: ,你不需要for (UITouch *touch in touches)循环,因为你已经有了self.trackingTouch的引用,所以你可以直接使用它。

如果我正确地理解了你的问题,每当用户在你的canvas上使用多个手指时,你的笔触就会不受欢迎。 既然你已经有一个self.trackingTouch ,是否有理由忽略其他不是self.trackingTouch其他触摸?

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (self.trackingTouch) { // another touch is active so we ignore this one return; } // ...the rest of your code } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = self.trackingTouch; if (![touches containsObject:touch]) { // updates to touches other than self.trackingTouch return; } // ...process only self.trackingTouch in the rest of your code } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = self.trackingTouch; if (![touches containsObject:touch]) { // updates to touches other than self.trackingTouch return; } // ...process only self.trackingTouch in the rest of your code self.trackingTouch = nil; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { // ...same as touchesEnded:withEvent: }