简体   繁体   English

在UIView或GLKView上绘制?

[英]Draw on UIView or GLKView?

I need to draw a separator line with some dots on it. 我需要画一条分隔线上有一些点。 I have decided I will do this using the draw method, as opposed to including images of the separator. 我决定我将使用draw方法执行此操作,而不是包括分隔符的图像。 I will do this for performance and for customisability, as the separator changes sometimes. 我会这样做是为了提高性能和可定制性,因为分隔符有时会更改。

Now I have looked into the draw() method on UIView and I have noticed that Apple suggests using GLKView when drawing using OpenGL. 现在,我已经研究了UIView上的draw()方法,并且注意到Apple建议在使用OpenGL进行GLKView时建议使用GLKView

For a simple separator, won't it be too much of a hassle to call OpenGL? 对于一个简单的分隔符,调用OpenGL会不会很麻烦? Or is the OpenGL overhead negligible? 还是OpenGL的开销可以忽略不计? When would I want to use the native UIKit draw() then? 那我什么时候要使用本机UIKit draw()

FYI I don't know either method, but want to learn both methods, so don't reply "what you know best". 仅供参考,我不知道任何一种方法,但是想学习这两种方法,所以不要回答“您最了解的内容”。 I am simply asking about performance. 我只是问性能。

OpenGL uses GPU instead of CPU for computation. OpenGL使用GPU而不是CPU进行计算。 If you are making something like a gaming app, then you can think of using OpenGL. 如果您正在制作类似游戏应用程序的东西,那么您可以考虑使用OpenGL。 I believe you want to draw a line in an iOS App. 我相信您想在iOS应用中画一条线。 For that you can either use drawRect method in UIView or create a shapeLayer and add it as a sublayer. 为此,您可以在UIView中使用drawRect方法,也可以创建shapeLayer并将其添加为子图层。

The following examples will show you: 以下示例将向您显示:

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

For using drawRect , you are supposed to do this inside a Custom UIView as opposed to the above method. 对于使用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];
}

If your separator parameters changes and if you are making an app, it's better to use drawRect method. 如果分隔符参数发生变化并且正在制作应用程序,则最好使用drawRect方法。 You can call this method anytime by using [CustomUIView setNeedsDisplay:YES] 您可以随时使用[CustomUIView setNeedsDisplay:YES]调用此方法。

Edit 编辑

What you're asking for is circle over line. 您要的是在线上画圈。 You can do that by drawing UIBezierPath for line first and then add UIBezierPath for circle later. 您可以通过UIBezierPathline绘制UIBezierPath ,然后为circle添加UIBezierPath来实现。

Normal Line 法线

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
[path addLineToPoint:CGPointMake(100.0, 100.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:shapeLayer];

Dotted Line 虚线

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
[path addLineToPoint:CGPointMake(100.0, 100.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
shapeLayer.lineDashPattern = @[@4, @2];
[self.view.layer addSublayer:shapeLayer];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM