使用 Quartz 绘制/移动文本的最佳实践是什么?

发布于 2024-10-06 00:43:14 字数 840 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

轻拂→两袖风尘 2024-10-13 00:43:15

我已经找到了答案,我使用 NSString:drawInRect 而不是 UILable:drawTextInRect :

-(void)drawRect:(CGRect)rect  
{    
 // Draw many strings like this ...    
 CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 0.5);   
 CGRect rect = CGRectMake(x, y, [self getSizeX], [self getSizeY]); 
 [character drawInRect:rect withFont:[UIFont fontWithName:@"Courier" size:fontSize]];
}

I have figured out the answer, I used NSString:drawInRect instead of UILable:drawTextInRect :

-(void)drawRect:(CGRect)rect  
{    
 // Draw many strings like this ...    
 CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 0.5);   
 CGRect rect = CGRectMake(x, y, [self getSizeX], [self getSizeY]); 
 [character drawInRect:rect withFont:[UIFont fontWithName:@"Courier" size:fontSize]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文