ICL1901
ICL1901

Reputation: 7778

Can I offset the touch points in Objective C?

Is it possible to offset UITouch points? I am using the touches methods shown below. I want to offset the touch points by some number so that a user will see where the touch will start and draw. If I am trying to draw within lines, I want to see above my fingertip, much like we do with pens and pencils.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    // add the first touch
    UITouch *touch = [touches anyObject];
    
    previousPoint = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];
  
    // init the bezier path
    self.currentTool = [self toolWithCurrentSettings];
    self.currentTool.lineWidth = self.lineWidth;
    self.currentTool.lineColor = self.lineColor;
    self.currentTool.lineAlpha = self.lineAlpha;
    
    [self.currentTool setInitialPoint:currentPoint];
...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    // save all the touches in the path
    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];
    
    //currentPoint.y += 40;
     CGRect bounds = [(DrawingPenTool*)self.currentTool addPathPreviousPoint:previousPoint2 withPreviousPoint:previousPoint1 withCurrentPoint:currentPoint];

        CGRect drawBox = bounds;
        drawBox.origin.x -= self.lineWidth * 2.0;
        drawBox.origin.y -= self.lineWidth * 2.0;
        drawBox.size.width += self.lineWidth * 4.0;
        drawBox.size.height += self.lineWidth * 4.0;
        
        [self setNeedsDisplayInRect:drawBox];    
        [self.currentTool moveFromPoint:previousPoint1 toPoint:currentPoint];
        [self setNeedsDisplay];
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    [self touchesMoved:touches withEvent:event];
    
      CGPoint point = [[touches anyObject] locationInView:self];
      [self.currentTool setInitialPoint:point];
    
     
        }
    
}

Upvotes: 0

Views: 157

Answers (1)

matt
matt

Reputation: 535230

You can use UIEvent predictedTouches and UITouch estimatedProperties to learn where the next touch will probably be. In this way you can keep drawing "ahead" of the user. That is exactly what this feature is for.

Upvotes: 2

Related Questions