在UIView中画线

我需要在UIView中画一条水平线。 什么是最简单的方法来做到这一点。 例如,我想在y-coord = 200处画一条黑色的水平线。

我没有使用Interface Builder。

在你的情况(水平线)最简单的方法是添加一个黑色的背景颜色和框架[0, 200, 320, 1] 0,200,320,1]的子视图。

代码示例(我希望没有错误 – 我没有Xcode编写):

 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)]; lineView.backgroundColor = [UIColor blackColor]; [self.view addSubview:lineView]; [lineView release]; // You might also keep a reference to this view // if you are about to change its coordinates. // Just create a member and a property for this... 

另一种方法是创build一个将在drawRect方法中绘制一条线的类(您可以在这里看到我的代码示例)。

也许这有点晚,但我想补充说,有一个更好的办法。 使用UIView很简单,但相对较慢。 这种方法会覆盖视图自身的画法,速度更快:

 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); // Draw them with a 2.0 stroke width so they are a bit more visible. CGContextSetLineWidth(context, 2.0f); CGContextMoveToPoint(context, 0.0f, 0.0f); //start at this point CGContextAddLineToPoint(context, 20.0f, 20.0f); //draw to this point // and now draw the Path! CGContextStrokePath(context); } 

只需添加一个没有文字和背景颜色的标签。 设置您所select的坐标以及高度和宽度。 您可以手动或使用Interface Builder来完成。

你可以使用这个UIBezierPath类:

并且可以根据需要绘制尽可能多的线条:

我已经分类UIView:

  @interface MyLineDrawingView() { NSMutableArray *pathArray; NSMutableDictionary *dict_path; CGPoint startPoint, endPoint; } @property (nonatomic,retain) UIBezierPath *myPath; @end 

并初始化将用于绘制线条的pathArray和dictPAth对象。 我从我自己的项目中编写代码的主要部分:

 - (void)drawRect:(CGRect)rect { for(NSDictionary *_pathDict in pathArray) { [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor) [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; } [[dict_path objectForKey:@"color"] setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor) [[dict_path objectForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; } 

touchesBegin方法:

 UITouch *touch = [touches anyObject]; startPoint = [touch locationInView:self]; myPath=[[UIBezierPath alloc]init]; myPath.lineWidth = currentSliderValue*2; dict_path = [[NSMutableDictionary alloc] init]; 

touchesMoved方法:

 UITouch *touch = [touches anyObject]; endPoint = [touch locationInView:self]; [myPath removeAllPoints]; [dict_path removeAllObjects];// remove prev object in dict (this dict is used for current drawing, All past drawings are managed by pathArry) // actual drawing [myPath moveToPoint:startPoint]; [myPath addLineToPoint:endPoint]; [dict_path setValue:myPath forKey:@"path"]; [dict_path setValue:strokeColor forKey:@"color"]; // NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path]; // [pathArray addObject:tempDict]; // [dict_path removeAllObjects]; [self setNeedsDisplay]; 

touchesEnded方法:

  NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path]; [pathArray addObject:tempDict]; [dict_path removeAllObjects]; [self setNeedsDisplay]; 

另外一个(甚至更短)的可能性。 如果你在drawRect里面,像下面这样:

 [[UIColor blackColor] setFill]; UIRectFill((CGRect){0,200,rect.size.width,1}); 

Swift 3

这就是你如何绘制一个灰色的线在你的看法(与b123400的答案相同的想法)

 class CustomView: UIView { override func draw(_ rect: CGRect) { super.draw(rect) let context = UIGraphicsGetCurrentContext() context?.setStrokeColor(UIColor.gray.cgColor) context?.setLineWidth(1) context?.move(to: CGPoint(x: 0, y: bounds.height)) context?.addLine(to: CGPoint(x: bounds.width, y: bounds.height)) context?.strokePath() } } 

根据Guy Daher的回答。

我尽量避免使用? 因为如果GetCurrentContext()返回nil,它可能导致应用程序崩溃。

我会做无核查声明:

 class CustomView: UIView { override func draw(_ rect: CGRect) { super.draw(rect) if let context = UIGraphicsGetCurrentContext() { context.setStrokeColor(UIColor.gray.cgColor) context.setLineWidth(1) context.move(to: CGPoint(x: 0, y: bounds.height)) context.addLine(to: CGPoint(x: bounds.width, y: bounds.height)) context.strokePath() } } } 

添加没有文字和背景颜色相应的框架大小(例如:高度= 1)的标签。 通过代码或界面构build器来完成。