使用 Quartz 绘制/移动文本的最佳实践是什么?
我是 iPhone 开发新手,我想实现一个简单的应用程序,可以在视图中显示和移动一些文本。所以我使用下面的代码来实现主循环:
- (id)initWithCoder:(NSCoder*)coder {
[NSTimerscheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 30.0))
target:selfselector:@selector(mainLoop) userInfo:nilrepeats:TRUE];
}
-(void) mainLoop {
// Do some updates
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// Draw many strings like this ...
UILabel *label = [[UILabel alloc] init];
label.textColor = color;
label.font = font;
label.text = character;
CGRect rect = CGRectMake(posX, posY, width, height);
[label drawTextInRect:rect];
}
但是当屏幕上的文本超过100个时,屏幕刷新会很慢并且不流畅。我想知道我画的方法是否正确?我在网上搜索了很多文档但没有找到答案。我发现了很多根据上下文绘制文本的方法,但我不知道应该遵循哪种方法。一件事是,我需要使用 UILabel 因为我需要设置颜色。 那么我的代码有什么问题吗?我应该使用 SubView 进行绘制,然后将 SubView 添加到主视图吗?或者我应该使用CGLayer?谁能告诉我这种程序的最佳实践是什么?谢谢!
I'm new to Iphone development, i want to implement a simple application that some texts can be shown and moved in the view. So i use the following code to implement the main loop:
- (id)initWithCoder:(NSCoder*)coder {
[NSTimerscheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 30.0))
target:selfselector:@selector(mainLoop) userInfo:nilrepeats:TRUE];
}
-(void) mainLoop {
// Do some updates
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// Draw many strings like this ...
UILabel *label = [[UILabel alloc] init];
label.textColor = color;
label.font = font;
label.text = character;
CGRect rect = CGRectMake(posX, posY, width, height);
[label drawTextInRect:rect];
}
But when there are more than 100 text on the screen, the screen refresh will be slow and not smooth. I want to know whether i'm doing the drawing in the right way? I have searched many documents on web but didnt find the answer. I found many ways to draw text on context but I don't know which I should follow. One thing is, I need to use UILabel since I need to set the color.
So is there any problem with my code? Should I use a SubView to draw and then addSubView to the main view? or should I use CGLayer? could anyone tell me what is the best practice for this kind of program? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了答案,我使用 NSString:drawInRect 而不是 UILable:drawTextInRect :
I have figured out the answer, I used NSString:drawInRect instead of UILable:drawTextInRect :