跟踪多点触控中的触摸点

我正在开发一个绘图工程,我想支持多点触控,我已经通过在线文档,build议跟踪接触点,我做到了,但我没有得到所需的行为。 我没有得到直线。

以下是我的代码

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. red = 0.0/255.0; green = 0.0/255.0; blue = 0.0/255.0; brush = 10.0; opacity = 1.0; self.view.multipleTouchEnabled = YES; touchPaths = [NSMutableDictionary dictionary]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { NSString *key = [NSString stringWithFormat:@"%d", (int) touch]; lastPoint = [touch locationInView:self.view]; [touchPaths setObject:[NSValue valueWithCGPoint:lastPoint] forKey:key]; } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { NSString *key = [NSString stringWithFormat:@"%d", (int) touch]; lastPoint = [[touchPaths objectForKey:key] CGPointValue]; CGPoint currentPoint1 = [touch locationInView:self.view]; UIGraphicsBeginImageContext(self.view.frame.size); [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint1.x, currentPoint1.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush ); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); CGContextStrokePath(UIGraphicsGetCurrentContext()); self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); [self.tempDrawImage setAlpha:opacity]; UIGraphicsEndImageContext(); lastPoint = currentPoint1; } } 

但是,当我使用这个代码绘制,我得到了这个。 在这里输入图像说明

所以朋友们,请帮助我,我做错了什么。

关心Ranjit

您没有正确填充touchPaths 。 尝试在每个绘图之后设置它,如下所示:

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { NSString *key = [NSString stringWithFormat:@"%d", (int) touch]; CGPoint lastPoint = [[touchPaths objectForKey:key] CGPointValue]; CGPoint currentPoint1 = [touch locationInView:self.view]; UIGraphicsBeginImageContext(self.view.frame.size); [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint1.x, currentPoint1.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush ); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); CGContextStrokePath(UIGraphicsGetCurrentContext()); self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); [self.tempDrawImage setAlpha:opacity]; UIGraphicsEndImageContext(); // I changed your code here [touchPaths setObject:[NSValue valueWithCGPoint:currentPoint1] forKey:key]; } } 

您目前正在设置lastPoint ,但您似乎并没有使用它(只能一触即可)。 也没有必要有lastPoint作为一个领域。

我尽可能使用手势识别器。 这里我使用UIPanGestureRecognizer

 - (void)viewDidLoad { [super viewDidLoad]; [self.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)]]; } - (void)didPan:(UIPanGestureRecognizer*)panGesture { for (NSUInteger touchIndex = 0; touchIndex < panGesture.numberOfTouches; touchIndex++) { // touchIndex is basically the "source" (the finger) from which the point comes from CGPoint p = [panGesture locationOfTouch:touchIndex inView:self.view]; [self drawAtPoint:p withIndex:touchIndex]; } } - (void)drawAtPoint:(CGPoint)point withIndex:(NSUInteger)index{ UIView *smallPoint = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, 3, 3)]; [smallPoint setBackgroundColor:[self colorForIndex:index]]; [self.view addSubview:smallPoint]; } - (UIColor*)colorForIndex:(NSUInteger)index { switch (index) { case 0: return [UIColor redColor]; case 1: return [UIColor orangeColor]; case 2: return [UIColor yellowColor]; case 3: return [UIColor blueColor]; case 4: return [UIColor greenColor]; } return [UIColor clearColor]; } 

我没有绘制一条更加缓慢的path,但是如果将它放在一个空的ViewController中并运行它,您将看到当多次触摸屏幕时,每个手指绘制的颜色都不相同。

所以如果考虑touchIndex,基本上你可以跟踪不同path的不同手指。

假设您使用两根手指绘制:您将拥有panGesture.numberOfTouches == 2 ,并且touchIndex 0将代表第一根手指, touchIndex 1将代表第二根手指。 您可以在不同的arrays中累积点,并将点添加到相应的path。