[英]Draw on UIView or GLKView?
我需要畫一條分隔線上有一些點。 我決定我將使用draw方法執行此操作,而不是包括分隔符的圖像。 我會這樣做是為了提高性能和可定制性,因為分隔符有時會更改。
現在,我已經研究了UIView
上的draw()
方法,並且注意到Apple建議在使用OpenGL進行GLKView
時建議使用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]
調用此方法。
編輯
您要的是在線上畫圈。 您可以通過UIBezierPath
為line
繪制UIBezierPath
,然后為circle
添加UIBezierPath
來實現。
法線
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];
虛線
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.