Ios 图形UIView与图形CGContext路径
我想知道在使用Ios 图形UIView与图形CGContext路径,ios,objective-c,performance,uiview,cgcontext,Ios,Objective C,Performance,Uiview,Cgcontext,我想知道在使用 ui视图: UIView *box = [[UIView alloc] initWithFrame:CGRectMake(ax, ay, width, height)]; box.backgroundColor = [UIColor clearColor]; box.layer.borderColor = [UIColor whiteColor].CGColor; box.layer.borderWidth = lineWidth; [self.view addSubview:b
ui视图
:
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(ax, ay, width, height)];
box.backgroundColor = [UIColor clearColor];
box.layer.borderColor = [UIColor whiteColor].CGColor;
box.layer.borderWidth = lineWidth;
[self.view addSubview:box];
或CGContext
:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, lineWidth);
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextMoveToPoint(context, ax, ay);
CGContextAddLineToPoint(context, ax+width, ay);
CGContextAddLineToPoint(context, ax+width, ay+height);
CGContextAddLineToPoint(context, ax, ay+height);
CGContextAddLineToPoint(context, ax, ay);
CGContextStrokePath(context);
这完全取决于你想要实现什么。如果你画了很多矩形,你可能应该选择
CGContext
的方式。如果它只是一个围绕其他视图元素的矩形,那么最好使用UIView方法。
一般来说,我认为不鼓励使用UIViews在屏幕上绘制内容,因此当有疑问时,使用CGContext这个问题的唯一答案应该是测量并查看对于您的确切用例来说什么是最有效的。