Track touch points in Multitouch

后端 未结 2 627
不思量自难忘°
不思量自难忘° 2020-12-11 18:11

I am working on a drawing project, I want to support multitouch, I have gone through documentation online, which suggest to track the touch points, I did it , But I am not g

2条回答
  •  情书的邮戳
    2020-12-11 19:00

    You are not populating the touchPaths properly. Try setting it after each drawing instead, something like this:

    - (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];
    
        }
    }
    

    You are currently setting lastPoint here but you do not seem to use it (and it would only work with one touch). There is no need to have lastPoint as a field either.

提交回复
热议问题