我是 objective-c 的初学者,我试图制作具有Paint风格的程序,并且正在使用平移手势绘制一条线。我可以做出手势,但问题是,每次重新加载之前删除鼠标的位置时,我都无法在鼠标经过的位置后面拖动。救命!非常感谢!
这是该部分中的代码:

-(void)pan: (UIPanGestureRecognizer*)panGesture
{
    if(panGesture.state == UIGestureRecognizerStateChanged) {
        _panLocation = [panGesture locationInView:self];
    }
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawLine:context];
}

-(void)drawLine:(CGContextRef)context {
    if (self.tag == 0) {
        [[UIColor blackColor] setFill];
        UIGraphicsPushContext(context);
        CGContextBeginPath(context);
        CGContextAddArc(context, _panLocation.x, _panLocation.y, 4, 0, 2*M_PI, YES);
        CGContextSetAlpha(context, 1);
        CGContextFillPath(context);
    }
}

最佳答案

我想这会有所帮助
请参考draw line with gesture链接,其中使用UIPanGestureRecognizer进行绘制。
否则,您可以使用触摸委托方法执行此操作
为此,请点击链接
draw line by touch

09-25 21:44