IOS画线的问题

很多控件中希望加一条线, 常见横的竖的, 比如在一个页面的header里, cell中, 像tableview那样那样的分割线.

所以我总结了几种方式供大家参考:

1. 利用CGContext去画, 举个例子:

UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:imageView];  

    self.view.backgroundColor=[UIColor blueColor];  

    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);  //线宽
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);  //颜色
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100, 100);  //起点坐标
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200, 100);   //终点坐标
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageView.image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

2. 利用UIView, 我看到过有同学直接设置UIView宽或高为1来做线条:

UIView *line_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 1.f)];
    [line_view setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:line_view];

这样就是一条红色的横线lol

3. 利用UIImageView:

UIImage *separatorImage = [UIImage imageWithRenderColor:[UIColor redColor] renderSize:CGSizeMake(320.f, 1)];
        UIImageView *topSeparatorView = [[UIImageView alloc] initWithImage:separatorImage];
        topSeparatorView.center = CGPointMake(320.f*0.5, 0.5);
        [self addSubview:topSeparatorView];

这其实就是加入一张图片.

上述就是总结的三种横线画法, 当然还有其他很多方式, 有想法的可以留言.

时间: 2024-08-06 10:11:18

IOS画线的问题的相关文章

[ios]ios画线 UIGraphicsBeginImageContextWithOptions--生成透明的图形

参考 :http://www.mgenware.com/blog/?p=493 这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的差别. 我们将主要使用这3个类型,绘制出同一张图片,如下,一个笑脸: 首先使用Quartz的CGPath来做这张图.很简单,首先创建用于转移坐标的Transform,然后创建一个CGMutablePathRef(属于CG

iOS 画线

@interface ShowLineView : UIView -(void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineCap(context, kCGLineCapSquare);//设置线条样式 CGContextSetLineWidth(context, 0.2);    //设置线条粗细宽度 CGContextSetFillColorWithC

[修复] Firemonkey 画线问题(Android & iOS 平台)

问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing problemshttps://quality.embarcadero.com/browse/RSP-14309 适用:所有 Firemonkey 版本 for Android & iOS 修复方法: 请将源码 FMX.StrokeBuilder.pas 复制到自己的工程目录里,再进行修改. Step1: 找到下

IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等

转自:http://blog.csdn.net/zhibudefeng/article/details/8463268 [cpp] view plain copy // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { CGC

【转】IOS Quartz 各种绘制图形用法-实现画图片、写文字、画线、椭圆、矩形、棱形等

// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); /*NO.1画一条线 CGContextSetRGBStrokeCo

unity3d GL画线/物体跟随/坐标系转换

看见标题的人是不是在想... 一个小小的GL画线难吗? 一个小小的物体跟随难吗? 嗯,的确,一点不难.... 我一开始也是像你们那样想的,但是实际操作起来,还是和理论有区别的 写这个demo起因是这样的: 面试到了一家虚拟现实的公司,因为没有去公司 网上直接谈的,谈妥了hr估计是想看看我能不能胜任 给了我一张效果图,让我去实现画线的功能 咳咳,要求还是比较细致的,这里我们后面说 废话不多说,老规矩,先上效果图,然后直接进入主题 第一张是hr给我的图,第二张是我自己实现的 需求如下: 1.模型是旋

图形学_画线算法(DDA、Bresenham)

1. DDA算法实现直线绘制(需先安装easyx,百度下载即可) 1 #include "easyx.h" 2 #include "math.h" 3 #include "windows.h" 4 #include "stdio.h" 5 #include "stdlib.h" 6 #include "conio.h" 7 #include "graphics.h"

unity3d NavMeshAgent 寻路画线/画路径

今天在群里看见有个小伙在问Game视图寻路时怎么画线 正好前几天写了个寻路,而且自己也不知道具体怎么在寻路时画线,所以决定帮帮他,自己也好学习一下 在百度查了一下资料,直接搜寻路画路径.寻路画线...... 我可不是伸手党,我只是想看看别人是怎么实现的 结果什么都没有搜到!!那就直接搜unity3d 画线吧.....  果然很多资料!! Debug.DrawLine:使用这个函数只能在 screen 中看见画的线,在 game 中看不见 那我们要怎么在game中画线呢 百度给我答案:LineRe

Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等

// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    CGContextRef context = UIGraphicsGetCurrentContext();         /*NO.1画一条线     CGContex