画线的UIView画线的UIView(Draw line in UIView)

2019-05-13 06:09发布

我需要绘制一条水平线在一个UIView。 什么是做到这一点的最简单的方法。 例如,我想在的y坐标绘制黑色水平线= 200。

我不使用界面生成器。

Answer 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...

另一种方法是创建一个类,将在其的drawRect方法画一条线(你可以看到我这个代码示例在这里 )。



Answer 2:

也许这是一个有点晚了,但我想补充一点,有一个更好的办法。 使用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);
}


Answer 3:

迅速3和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()
        }
    }
}


Answer 4:

只需添加一个Label没有文字和背景颜色。 设置你的选择,也高度和宽度的坐标。 您可以手动或使用界面生成器做。



Answer 5:

可以为此用户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];


Answer 6:

一个其它(和甚至更短的)可能性。 如果你在里面的drawRect,类似如下:

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


Answer 7:

基于盖伊达希尔的答案。

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

我会做零检查if语句:

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()
        }
    }
}


Answer 8:

添加标签没有文字和与对应帧大小背景颜色(例如:高度= 1)。 做到这一点通过代码或在Interface Builder。



文章来源: Draw line in UIView