在UIView中画线

Joh*_*ith 84 iphone uiview ios

我需要在UIView中绘制一条水平线.什么是最简单的方法.例如,我想在y-coord = 200处绘制黑色水平线.

我不使用Interface Builder.

b12*_*400 311

也许这有点晚了,但我想补充说有更好的方法.使用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);
}
Run Code Online (Sandbox Code Playgroud)

  • 使用视图来做到这一点并不太可怕.它使事情变得更容易,例如,在方向更改期间执行隐式动画时.如果它只是一个,另一个UIView并不是那么昂贵,而且代码更简单. (37认同)

Mic*_*ler 120

在您的情况下(水平线)最简单的方法是添加黑色背景颜色和框架的子视图[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...
Run Code Online (Sandbox Code Playgroud)

另一种方法是创建一个将在其drawRect方法中绘制一条线的类(您可以在此处查看我的代码示例).

  • @coolcool1994,您有没有注意到“我没有使用 Interface Builder”。问题中的要求? (3认同)

Guy*_*her 25

Swift 3和Swift 4

这就是你如何在视图的末尾画一条灰线(与b123400的答案相同)

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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

只需添加没有文字和背景颜色的标签.设置您选择的坐标以及高度和宽度.您可以手动或使用Interface Builder执行此操作.


Rak*_*esh 11

您可以为此使用UIBezierPath类:

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

我有UIView的子类:

    @interface MyLineDrawingView()
    {
       NSMutableArray *pathArray;
       NSMutableDictionary *dict_path;
       CGPoint startPoint, endPoint;
    }

       @property (nonatomic,retain)   UIBezierPath *myPath;
    @end
Run Code Online (Sandbox Code Playgroud)

并初始化将用于线条绘制的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];

}
Run Code Online (Sandbox Code Playgroud)

touchesBegin方法:

UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:self];
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth = currentSliderValue*2;
dict_path = [[NSMutableDictionary alloc] init];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

touchesEnded方法:

        NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path];
        [pathArray addObject:tempDict];
        [dict_path removeAllObjects];
        [self setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)


hka*_*atz 11

另一种(甚至更短)的可能性.如果您在drawRect中,请执行以下操作:

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