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);

- (void)drawRect:(CGRect)rect {

//线段
    //1.创建图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(ctx, 10);//1.1设置线条的宽度

//    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);//1.2设置线条颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
   
    CGContextSetLineCap(ctx, kCGLineCapRound);//设置线条顶部和尾部的样式

CGContextSetLineJoin(ctx, kCGLineJoinRound);//设置转折点的样式

//2.进行图形绘制
    CGContextMoveToPoint(ctx, 30, 30);
    CGContextAddLineToPoint(ctx, 70, 100);
    CGContextAddLineToPoint(ctx, 200, 30);
   
    //3.输出目标
    CGContextStrokePath(ctx);//第一次渲染
   
    //画第二根线段
    CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(ctx, 30, 30);
    CGContextAddLineToPoint(ctx, 200, 30);
    CGContextStrokePath(ctx);//第二次渲染

}

#pragma mark 圆弧
void drawARC(){
   
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, 144, 100, 100, M_PI, M_PI_2, 1);
    CGContextStrokePath(ctx);
//    CGContextAddArc(上下文, 圆心X, 圆心Y, 半径, 开始角度, 结束角度, 顺逆时针——0顺1逆);

}

#pragma mark 圆形
void drawCircle(){

CGContextRef ctx = UIGraphicsGetCurrentContext();

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

CGContextSetLineWidth(ctx, 10);

CGContextStrokePath(ctx);

}

#pragma mark 三角形
void draw3Rect(){
    //1.图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 10);
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
   
    //2.图形绘制
    CGContextMoveToPoint(ctx, 100, 20);
    CGContextAddLineToPoint(ctx, 20, 50);

CGContextAddLineToPoint(ctx, 220, 220);

//3.渲染(绘制)
//    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);
    CGContextClosePath(ctx);//无法实现闭合

}

时间: 2024-08-07 12:55:31

DrawRect的相关文章

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

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

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就开始画了.这样可以在控制器中设置一些值给