Ios 如何使用Quartz2d在iPhone中绘制图片

Ios 如何使用Quartz2d在iPhone中绘制图片,ios,quartz-2d,Ios,Quartz 2d,我现在正在学习石英,想做一个如下演示: 当你的手指在iPhone屏幕上移动时,它会以红色显示轨迹 代码如下: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; _firstPoint = [touch locationInView:self]; } - (void)touchesMoved:(NSSet *)touches withEve

我现在正在学习石英,想做一个如下演示: 当你的手指在iPhone屏幕上移动时,它会以红色显示轨迹 代码如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_firstPoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_endPoint = [touch locationInView:self];
[self setNeedsDisplay];
_firstPoint = _endPoint;
 }
然后

这里,_firstPoint和_endPoint是记录位置的CGPoint。 但是,它没有显示轨迹。 我不知道是什么问题。 请给我一些提示

最后,我想咨询一下实现这种应用程序是否正确


谢谢

您可以阅读本教程来了解它-可能有帮助吗

我想你首先错过了CGContextBeginPath(…)


祝你好运

关于组成线的点集合的存储位置,本例中不存储

编辑

是的,为了存储它们,我只需要添加到NSMutableArray

差不多

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!_points) _points = [[NSMutableArray array] retain];
     UITouch *touch = [touches anyObject];
    [_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
    [self setNeedsDisplay];
 }

setNeedsDisplay将调用drawRect,您可以在这里使用点和绘制方法。

链接可以工作。但是我想知道我的代码有什么问题。谢谢!我不太明白。我是否应该存储所有点并在TouchesEnded中绘制它们?我想如果我移动,我会把点添加到屏幕上,画成一条线。我试过了,效果很好。非常感谢。还有一个问题:在这种情况下,当我移动时,原始上下文的某些部分会受到影响。如何避免这种情况?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!_points) _points = [[NSMutableArray array] retain];
     UITouch *touch = [touches anyObject];
    [_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
    [self setNeedsDisplay];
 }