drawrect的使用

通过对ios开发ui篇的学习 总结了drawrect的方法和使用

新建一个项目,自定义一个view类和storyboard关联后,重写该类中的drowrect方法。

画线的三个步骤:

(1)获取上下文

(2)绘图

(3)渲染

画直线代码:

- (void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //绘图
    //第一条线
    CGContextMoveToPoint(ctx, 20, 100);
    CGContextAddLineToPoint(ctx, 100, 320);

    //第二条线
    CGContextMoveToPoint(ctx, 40, 200);
    CGContextAddLineToPoint(ctx, 80, 100);
    //渲染
    CGContextStrokePath(ctx);

}

带颜色的直线代码:

- (void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //绘图
    //第一条线
    CGContextMoveToPoint(ctx, 20, 100);
    CGContextAddLineToPoint(ctx, 100, 320);

    //设置第一条线的状态
    //设置线条的宽度
    CGContextSetLineWidth(ctx, 12);
    //设置线条的颜色
    [[UIColor brownColor]set];
    //设置线条两端的样式为圆角
    CGContextSetLineCap(ctx,kCGLineCapRound);
    //对线条进行渲染
    CGContextStrokePath(ctx);

    //第二条线
    CGContextMoveToPoint(ctx, 40, 200);
    CGContextAddLineToPoint(ctx, 80, 100);
    //渲染
    CGContextStrokePath(ctx);

}

一条带颜色,一条为原始的代码:方法一

- (void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //绘图
    //第一条线
    CGContextMoveToPoint(ctx, 20, 100);
    CGContextAddLineToPoint(ctx, 100, 320);

    //设置第一条线的状态
    //设置线条的宽度
    CGContextSetLineWidth(ctx, 12);
    //设置线条的颜色
    [[UIColor brownColor]set];
    //设置线条两端的样式为圆角
    CGContextSetLineCap(ctx,kCGLineCapRound);
    //对线条进行渲染
    CGContextStrokePath(ctx);

    //第二条线
    CGContextMoveToPoint(ctx, 40, 200);
    CGContextAddLineToPoint(ctx, 80, 100);

    //清空状态
    CGContextSetLineWidth(ctx, 1);
    [[UIColor blackColor]set];
    CGContextSetLineCap(ctx,kCGLineCapButt);

    //渲染
    CGContextStrokePath(ctx);

}

方法二:

- (void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //绘图

    //第二条线
    CGContextMoveToPoint(ctx, 40, 200);
    CGContextAddLineToPoint(ctx, 80, 100);

    //清空状态
    //    CGContextSetLineWidth(ctx, 1);
    //    [[UIColor blackColor]set];

    //    CGContextSetLineCap(ctx,kCGLineCapButt);

    //渲染
    CGContextStrokePath(ctx);

    //第一条线
    CGContextMoveToPoint(ctx, 20, 100);
    CGContextAddLineToPoint(ctx, 100, 320);

    //设置第一条线的状态
    //设置线条的宽度
    CGContextSetLineWidth(ctx, 12);
    //设置线条的颜色
    [[UIColor brownColor]set];
    //设置线条两端的样式为圆角
    CGContextSetLineCap(ctx,kCGLineCapRound);
    //对线条进行渲染
    CGContextStrokePath(ctx);
}

画四边形代码:

- (void)drawRect:(CGRect)rect
{
    //画四边形
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //绘图
    CGContextAddRect(ctx, CGRectMake(20, 50, 100, 100));
    //渲染
    CGContextStrokePath(ctx);
}

画一个歪的四边形:

- (void)drawRect:(CGRect)rect
{
    //画四边形
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //矩阵操作
    //注意点:设置矩阵操作必须要在添加绘图信息之前
    //旋转45度
    CGContextRotateCTM(ctx, M_PI_4);

    //绘图
    CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100));
    //渲染
    CGContextStrokePath(ctx);
}

画一个圆代码以及旋转代码:

- (void)drawRect:(CGRect)rect

{

//获取图形上下文

CGContextRef ctx=UIGraphicsGetCurrentContext();

//矩阵操作

//注意点:设置矩阵操作必须要在添加绘图信息之前

//旋转45度

//    CGContextRotateCTM(ctx, M_PI_4);

//绘图

//画四边形

CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100));

//画一个圆

CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50));

//渲染

CGContextStrokePath(ctx);

}

旋转代码:

- (void)drawRect:(CGRect)rect
{
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //矩阵操作
    //注意点:设置矩阵操作必须要在添加绘图信息之前
    //旋转45度
    CGContextRotateCTM(ctx, M_PI_4);

    //绘图
    //画四边形
    CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100));
    //画一个圆
    CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50));
    //渲染
    CGContextStrokePath(ctx);
}

缩放代码:

- (void)drawRect:(CGRect)rect
{
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //矩阵操作
    //注意点:设置矩阵操作必须要在添加绘图信息之前
    //缩放,x方向缩放0.5倍,y方向缩放1.5倍
    CGContextScaleCTM(ctx, 0.5, 1.5);

    //绘图
    //画四边形
    CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100));
    //画一个圆
    CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50));
    //渲染
    CGContextStrokePath(ctx);
}

平移代码:

- (void)drawRect:(CGRect)rect
{
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //矩阵操作
    //注意点:设置矩阵操作必须要在添加绘图信息之前
    //平移,x方向移动50,y方向移动100
    CGContextTranslateCTM(ctx, 50, 100);

    //绘图
    //画四边形
    CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100));
    //画一个圆
    CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50));
    //渲染
    CGContextStrokePath(ctx);
}

画一个圆形头像代码:

- (void)drawRect:(CGRect)rect
{
    //画圆,以便以后指定可以显示图片的范围
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));

    //指定上下文中可以显示内容的范围就是圆的范围
    CGContextClip(ctx);
    UIImage *image2=[UIImage imageNamed:@"me"];
    [image2 drawAtPoint:CGPointMake(100, 100)];
}

画一个三角形头像:

- (void)drawRect:(CGRect)rect
{

    //画三角形,以便以后指定可以显示图片的范围
    //获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
//    CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 60, 150);
     CGContextAddLineToPoint(ctx, 140, 150);
    CGContextClosePath(ctx);

    //注意:指定范围(也就是指定剪切的方法一定要在绘制范围之前进行调用)
    //指定上下文中可以显示内容的范围就是圆的范围
    CGContextClip(ctx);
    UIImage *image2=[UIImage imageNamed:@"me"];
    [image2 drawAtPoint:CGPointMake(100, 100)];
}
时间: 2024-08-09 21:58:43

drawrect的使用的相关文章

内存恶鬼drawRect - 谈画图功能的内存优化

正文 标题有点吓人,但是对于drawRect的评价倒是一点都不过分.在平日的开发中,随意覆盖drawRect方法,稍有不慎就会让你的程序内存暴增.下面我们来看一个例子. 做了一个画板功能,但是苦于内存问题一直得不到解决.画板功能很简单,就是记录手指触摸的轨迹然后绘制在屏幕上.下面我们来看一张效果图: 如图我们看到左侧内存的状况随着手指的绘制逐渐恶化.另外细心的朋友可以观察到,点击图中蓝色矩形按钮之后,便会弹出画板,而这时并没有进行任何的手指绘制,内存就突变为 114 MB ,然后每当手指绘制开始

DrawRect

//画贝塞尔曲线 CGContextSetLineWidth(ctx, 3); CGContextMoveToPoint(ctx, 120, 220);//开始的点 //CGContextAddQuadCurveToPoint(ctx, 切点X, 切点Y, 结束X, 结束Y); CGContextAddQuadCurveToPoint(ctx, 160, 250, 200, 220); [[UIColor blackColor] set]; CGContextStrokePath(ctx); -

layoutSubviews 与 drawRect

layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews.2.addSubview会触发layoutSubviews.3.设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化.4.滚动一个UIScrollView会触发layoutSubviews.5.旋转Screen会触发父UIView上的layoutSubviews事件.6.改变一个UIView大小的时候也会触发父UIView上的layoutS

iOS 开发 - 在哪里更新 custom view 的subviews 的位置信息? (drawRect: 还是 layoutSubviews ?)

对于custom,可以通过addSubview: 增加内容,也可以通过Core Graphic绘制内容. 对于后者,在drawRect: 进行,位置信息随之确定 对于前者,一般在view初始化时进行(也可以动态地在其它地方进行),设置subview的位置信息时,需要了解下面的区别: - 在 layoutSubviews中,此时custom view 的bounds虽然已经确定,但是对于有constraint的subview(比如在nib文件中添加的约束),其位置信息还没有按照constraint

iOS开发——UI篇OC篇&layoutSubviews和drawRect

layoutSubviews和drawRect 首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews. 2.addSubview会触发layoutSubviews. 3.设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化. 4.滚动一个UIScrollView会触发layoutSubviews

【学习ios之路:UI系列】绘图(drawRect)

实现代码如下: ①在自定义视图中定义3个属性 //记录线条的路径 @property (nonatomic, retain) NSMutableArray *paths; //设置绘图过程中线条的颜色 @property (nonatomic, retain) UIColor *pathColor; @property (nonatomic, assign) CGFloat pathWidth; //撤销功能 - (void)undo; //清除功能 - (void)clear; ②实现效果,需

graphics.drawRect()方法

drawRect方法的官方API文档描述 drawRect public void drawRect(int x, int y, int width, int height) Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height.

drawRect: 小注

drawRect:()默认是什么都不做的, 1.如果基础一个UIView,子类可以使用Core Graphics框架和UIKit在这个方法中完成绘制操作. 2.如果使用其他方法设置子类的content,可以不适用这个方法. 如:你只是改变背景颜色,或者使用他的underlying layer对象(包括直接使用子类的基本层,或者子类添加subview). 3.如果是直接从一个UIView对象(包括所有系统提供的UIView的子类)继承来的selfVIew,打开这个方法的时候可以不用call sup

iOS的layoutSubviews和drawRect方法何时调用

layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews.2.addSubview会触发layoutSubviews.3.设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化.4.滚动一个UIScrollView会触发layoutSubviews.5.旋转Screen会触发父UIView上的layoutSubviews事件.6.改变一个UIView大小的时候也会触发父UIView上的layoutS

使用drawRect有什么影响

用来画图,这个方法会在intiWithRect时候调用.这个方法的影响在于有touch event的时候之后,会重新绘制,很多这样的按钮的话就会比较影响效率.以下都会被调用1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用.drawRect 掉用是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在 控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给