Search code examples
ioscore-graphics

How can I make UITouches offset?


In the image below, I draw and the result is at point A, right where my finger touches.

enter image description here

How can I make the image appear about 40pt above my actual touch. (B)

I'm using the classic coreGraphic UITouch code, like so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    // add the first touch
    UITouch *touch = [touches anyObject];
    
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];
    
    //transform = CGAffineTransformTranslate(self.transform, 5.0, 10.0)
    
    // init the bezier path
    self.currentTool = [self toolWithCurrentSettings];
    self.currentTool.lineWidth = self.lineWidth;
    self.currentTool.lineColor = self.lineColor;
    self.currentTool.lineAlpha = self.lineAlpha;
    
    
        [self.pathArray addObject:self.currentTool];
        [self.undoStates addObject:[self.currentTool captureToolState]];
        
        [self.currentTool setInitialPoint:currentPoint];
    }
    
    // call the delegate
    if ([self.delegate respondsToSelector:@selector(drawingView:willBeginDrawUsingTool:)]) {
        [self.delegate drawingView:self willBeginDrawUsingTool:self.currentTool];
    }
}

- (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];
    
        [self.currentTool moveFromPoint:previousPoint1 toPoint:currentPoint];
        [self setNeedsDisplay];
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    // make sure a point is recorded
    [self touchesMoved:touches withEvent:event];
    
            CGPoint point = [[touches anyObject] locationInView:self];
            [self.currentTool setInitialPoint:point];
            self.draggableTextView = ((ACEDrawingDraggableTextTool *)self.currentTool).labelView;
            
            [self.pathArray addObject:self.currentTool];
            
            [self finishDrawing];
       
        [self finishDrawing];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    // make sure a point is recorded
    [self touchesEnded:touches withEvent:event];
}

Solution

  • previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];
    // add these 2 lines below:
    previousPoint1 = CGPointMake(previousPoint1.x, previousPoint1.y+40);
    currentPoint = CGPointMake(currentPoint.x, currentPoint.y+40);