如何使用 Quartz2d 在 iPhone 中画图

发布于 2024-10-22 03:00:53 字数 913 浏览 2 评论 0原文

我现在正在学习 Quartz,想做一个这样的演示: 当你的手指在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;
 }

then

- (void)drawRect:(CGRect)rect {

// Drawing code.
CGContextRef _context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(_context, 1, 0, 0, 1);
CGContextMoveToPoint(_context, _firstPoint.x, _firstPoint.y);
CGContextAddLineToPoint(_context, _endPoint.x, _endPoint.y);

CGContextStrokePath(_context);
}

这里,_firstPoint和_endPoint是记录位置的CGPoint。 但是,它不显示轨道。 我不知道出了什么问题。 请给出任何提示。

最后想咨询一下做这样的App是否合适。

谢谢!

I'm studying Quartz now and want to do a demo like this:
when your finger moves on the iPhone screen, it shows the track in red color。
The code is like:

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

then

- (void)drawRect:(CGRect)rect {

// Drawing code.
CGContextRef _context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(_context, 1, 0, 0, 1);
CGContextMoveToPoint(_context, _firstPoint.x, _firstPoint.y);
CGContextAddLineToPoint(_context, _endPoint.x, _endPoint.y);

CGContextStrokePath(_context);
}

Here,_firstPoint and _endPoint are CGPoint to record positions.
However, it doesn't show the track.
I don't know what is the problem.
Please give any tips.

Finally, I'd like to consultant whether it is right to fulfill such a kind of App.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-10-29 03:00:53

对于您关于构成线的点的集合存储在哪里的观点,它没有存储在本示例中。

编辑

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

像 setNeedsDisplay 之类的东西

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

将调用 drawRect ,您可以在其中使用点和绘制方法。

To your point about where the collection of points making up the lines is stored, it is not stored in this example.

EDITED

Yeah, to store them, I'd just add to a NSMutableArray.

something like

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

The setNeedsDisplay is going to invoke the drawRect that's where you use the points and your draw methods.

岁月如刀 2024-10-29 03:00:53

你可以阅读本教程来弄清楚 - 也许它会有所帮助

http://www .ifans.com/forums/showthread.php?t=132024

我认为您错过了 CGContextBeginPath(...) 首先祝您

好运!

you can read this tutorial to figured it out - may be it helps

http://www.ifans.com/forums/showthread.php?t=132024

i think you missed CGContextBeginPath(...) first of all

good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文