gpt4 book ai didi

ios - 在 UIView 或 GLKView 上绘制?

转载 作者:行者123 更新时间:2023-11-29 00:36:01 26 4
gpt4 key购买 nike

我需要绘制一 strip 有一些点的分隔线。我决定使用绘制方法来完成此操作,而不是包含分隔符的图像。我这样做是为了性能和可定制性,因为分隔符有时会发生变化。

现在我研究了 UIView 上的 draw() 方法,我注意到 Apple 建议在使用 OpenGL 绘图时使用 GLKView

对于一个简单的分隔符,调用OpenGL会不会太麻烦?还是 OpenGL 开销可以忽略不计?那么我什么时候想使用原生 UIKit draw() 呢?

仅供引用,我不知道这两种方法,但想学习这两种方法,所以不要回复“你最了解什么”。我只是问性能。

最佳答案

OpenGL 使用 GPU 而不是 CPU 进行计算。如果您正在制作游戏应用程序之类的东西,那么您可以考虑使用 OpenGL。我相信您想在 iOS 应用程序中画一条线。为此,您可以在 UIView 中使用 drawRect 方法或创建一个 shapeLayer 并将其添加为子层。

下面的例子会告诉你:

CAShapeLayer *simpleLine = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 80)];
[path addLineToPoint:CGPointMake(300, 80)];
simpleLine.lineWidth = 1.0;
simpleLine.path = path.CGPath;
simpleLine.strokeColor = [[UIColor blackColor] CGColor];
[[self.view layer] addSublayer:simpleLine];

要使用 drawRect,您应该在自定义 UIView 中执行此操作,而不是上述方法。

- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 80)];
[path addLineToPoint:CGPointMake(300, 80)];
path.lineWidth = 1.0;
[[UIColor blueColor] setStroke];
[path stroke];
}

如果您的分隔符参数发生变化并且您正在制作应用程序,则最好使用 drawRect 方法。您可以随时使用 [CustomUIView setNeedsDisplay:YES]

调用此方法

编辑

您要的是在线上的圆圈。您可以先为 line 绘制 UIBezierPath ,然后为 circle 添加 UIBezierPath

关于ios - 在 UIView 或 GLKView 上绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com