使用 CGContext 绘制线条

发布于 2024-10-23 17:02:17 字数 929 浏览 1 评论 0原文

我想在表视图单元格中画线,以便可以放置文本字段和文本字段。在单细胞中切换。我增加了单元格的高度。如何在单元格中画线?

我有 UIView 的子类,其中包含以下代码

 //Get the CGContext from this view
 CGContextRef context = UIGraphicsGetCurrentContext();
 //Set the stroke (pen) color
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
 //Set the width of the pen mark
 CGContextSetLineWidth(context, 1.0);

 // Draw a line
 //Start at this point
 CGContextMoveToPoint(context, 10.0, 30.0);

 //Give instructions to the CGContext
 //(move "pen" around the screen)
 CGContextAddLineToPoint(context, 310.0, 30.0);


 //Draw it
 CGContextStrokePath(context);

然后我有一个具有分组表格样式的 tableViewController 。在 cellForRowAtIndexPath 中,我有以下代码

//code to add textfield
DrawLineView *drawLine = [[DrawLineView alloc]init];
[cell addSubview:drawLine];
//code add switch

,但它没有绘制任何线条。我不能使用 2 个不同的电池。我必须请帮助我。这是我第一次在 iPhone 上处理图形。谢谢

I want to draw line in table view cell so that I can place textfield & switch in single cell. I increased the height of cell. How can I draw line in cell?

I have subclass of UIView which contains following code

 //Get the CGContext from this view
 CGContextRef context = UIGraphicsGetCurrentContext();
 //Set the stroke (pen) color
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
 //Set the width of the pen mark
 CGContextSetLineWidth(context, 1.0);

 // Draw a line
 //Start at this point
 CGContextMoveToPoint(context, 10.0, 30.0);

 //Give instructions to the CGContext
 //(move "pen" around the screen)
 CGContextAddLineToPoint(context, 310.0, 30.0);


 //Draw it
 CGContextStrokePath(context);

Then I have a tableViewController with grouped table style. In cellForRowAtIndexPath I have following code

//code to add textfield
DrawLineView *drawLine = [[DrawLineView alloc]init];
[cell addSubview:drawLine];
//code add switch

But it is not drawing any line. I can't use 2 different cells. I have to Please help me. This is my first to deal with graphics i iphone. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半步萧音过轻尘 2024-10-30 17:02:17

如果您只想画一条线,那么最好使用 CAShapeLayer,向其传递一条带有线的路径,然后将其附加为单元格内容视图的子图层。该表的性能应该比使用带有自定义绘制矩形的视图更好。

通过 CALayer 和路径绘制线条的示例:

// You'll also need the QuartzCore framework added to the project
#import <QuartzCore/QuartzCore.h>

CAShapeLayer *lineShape = nil;
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];

lineShape.lineWidth = 4.0f;
lineShape.lineCap = kCALineCapRound;;
lineShape.strokeColor = [[UIColor blackColor] CGColor];

int x = 0; int y = 0;
int toX = 30; int toY = 40;                            
CGPathMoveToPoint(linePath, NULL, x, y);
CGPathAddLineToPoint(linePath, NULL, toX, toY);

lineShape.path = linePath;
CGPathRelease(linePath);
[self.layer addSublayer:lineShape];

If all you want to do is draw a line, it would be a lot better to use a CAShapeLayer, pass it a path with a line, and then attach that as a sublayer of the cells content view. The table should perform better than using a view with a custom drawRect.

Example of drawing a line via CALayer and a path:

// You'll also need the QuartzCore framework added to the project
#import <QuartzCore/QuartzCore.h>

CAShapeLayer *lineShape = nil;
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];

lineShape.lineWidth = 4.0f;
lineShape.lineCap = kCALineCapRound;;
lineShape.strokeColor = [[UIColor blackColor] CGColor];

int x = 0; int y = 0;
int toX = 30; int toY = 40;                            
CGPathMoveToPoint(linePath, NULL, x, y);
CGPathAddLineToPoint(linePath, NULL, toX, toY);

lineShape.path = linePath;
CGPathRelease(linePath);
[self.layer addSublayer:lineShape];
做个ˇ局外人 2024-10-30 17:02:17

有两件事...
首先,您通常不会绘制单元格本身。

您通常会绘制到内容视图中。有时,绘制到单元格的backgroundView或selectedBackgroundView中是有意义的。

[cell.contentView addSubview:drawLine];

其次,默认的单元格文本标签cell.textLabel和cell.detailTextLabel具有不透明的背景。尝试将其背景颜色设置为[UIColor clearColor]

编辑:还有一件事:你需要为你的画线设置一个合适的框架

DrawLineView *drawLine = [[DrawLineView alloc]initWithFrame:cell.contentView.bounds];

Two things...
First, you usually don't draw into the cell itself.

You normally draw into the content view. Sometimes it makes sense draw into the cell's backgroundView or selectedBackgroundView.

[cell.contentView addSubview:drawLine];

Second, the default cell text labels cell.textLabel and cell.detailTextLabel have non-transparent background. Try setting their background colors to [UIColor clearColor].

Edit: one more thing: you need to set a proper frame for your drawLine

DrawLineView *drawLine = [[DrawLineView alloc]initWithFrame:cell.contentView.bounds];
多彩岁月 2024-10-30 17:02:17

我认为绘制线条的一种非常简单的方法是创建一个 UIView 并用所需的颜色填充它,然后相应地选择其宽度,并将高度设置为 1 像素,如下所示:

var lineView : UIView = {
     let view = UIView()
     view.backgroundColor = UIColor.blackColor()
     view.translatesAutoresizingMaskIntoConstraints = false
     return view
}()


self.view.addSubview(lineView)
self.view.addConstraints(NSLayoutConstraint.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": lineView])))
self.view.addConstraints(NSLayoutConstraint.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-50-[view(1)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": lineView])))

I think a very simplified way of drawing a line is to create a UIView and fill it with desired color, then choose its width accordingly, and setup height to be 1 pixel like so:

var lineView : UIView = {
     let view = UIView()
     view.backgroundColor = UIColor.blackColor()
     view.translatesAutoresizingMaskIntoConstraints = false
     return view
}()


self.view.addSubview(lineView)
self.view.addConstraints(NSLayoutConstraint.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": lineView])))
self.view.addConstraints(NSLayoutConstraint.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-50-[view(1)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["view": lineView])))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文