如何使用 Quartz2d 在 iPhone 中画图
我现在正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您关于构成线的点的集合存储在哪里的观点,它没有存储在本示例中。
编辑
是的,为了存储它们,我只需添加到 NSMutableArray 中。
像 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
The setNeedsDisplay is going to invoke the drawRect that's where you use the points and your draw methods.
你可以阅读本教程来弄清楚 - 也许它会有所帮助
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!