ios 我可以偏移Objective C中的触摸点吗?

hsgswve4  于 2023-05-30  发布在  iOS
关注(0)|答案(1)|浏览(188)

是否可以偏移UITouch点?我正在使用下面显示的触摸方法。我想通过一些数字来偏移触摸点,这样用户就可以看到触摸开始和绘制的位置。如果我试图在线条内绘画,我希望看到我的指尖上方,就像我们用钢笔和铅笔一样。

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

qni6mghb1#

您可以使用UIEvent predictedTouches和UITouch estimatedProperties来了解 * 下一次 * 触摸可能在哪里。通过这种方式,您可以保持在用户的“前面”进行绘制。这正是这个功能的作用。

相关问题