drawRect: 小注

drawRect:()默认是什么都不做的,

1.如果基础一个UIView,子类可以使用Core Graphics框架和UIKit在这个方法中完成绘制操作。

2.如果使用其他方法设置子类的content,可以不适用这个方法。

如:你只是改变背景颜色,或者使用他的underlying layer对象(包括直接使用子类的基本层,或者子类添加subview)。

3.如果是直接从一个UIView对象(包括所有系统提供的UIView的子类)继承来的selfVIew,打开这个方法的时候可以不用call super,但是如果你是继承一个自定义的UIView子类,这样myView:selfView,则如果myView需要使用到drawRect:方法的时候需要call super at some point in your implementation。

4.这个方法在第一次displayed view的时候,或者一个事件发生需要改变可见视图的时候被自动调用,你不能够直接调用这个方法,如果需要,可以使用方法setNeedsDisplay或者setNeedsDisplayInRect:替代方法来实现。

5、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。

6、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。

7、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕

——————————代码:

#import "InfoView.h"

@interface UIView (CanvasImageView)//类别加一个

//多边形

-(void)drawPolygon:(NSArray *)pointArray;

@end

@implementation UIView (CanvasImageView)

-(void)drawPolygon:(NSArray *)pointArray

{

//    NSAssert(pointArray.count>=2,@"数组长度必须大于等于2");

//    NSAssert([[pointArray[0] class] isSubclassOfClass:[NSValue class]], @"数组成员必须是CGPoint组成的NSValue");

CGContextRef     context = UIGraphicsGetCurrentContext();

NSValue *startPointValue = pointArray[0];

CGPoint  startPoint      = [startPointValue CGPointValue];

CGContextMoveToPoint(context, startPoint.x, startPoint.y);

for(int i = 1;i<pointArray.count;i++)

{

//        NSAssert([[pointArray[i] class] isSubclassOfClass:[NSValue class]], @"数组成员必须是CGPoint组成的NSValue");

NSValue *pointValue = pointArray[i];

CGPoint  point      = [pointValue CGPointValue];

CGContextAddLineToPoint(context, point.x,point.y);

}

[[UIColor redColor] setFill];

[[UIColor yellowColor] setStroke];

CGContextDrawPath(context, kCGPathFillStroke);

}

@end

@implementation InfoView

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// Initialization code

}

return self;

}

/* Only override drawRect: if you perform custom drawing. An empty implementation adversely affects performance during animation.*/

- (void)drawRect:(CGRect)rect

{

// Drawing code

CGPoint uppoint = CGPointMake(rect.size.width/2, rect.size.height/2-15);

CGPoint downpoint = CGPointMake(rect.size.width/2, rect.size.height/2+15);

CGPoint arrowpoint = CGPointMake(0, rect.size.height);

NSArray *pointArr = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:uppoint],[NSValue valueWithCGPoint:downpoint],[NSValue valueWithCGPoint: arrowpoint],[NSValue valueWithCGPoint:uppoint], nil];

//这一块代码绘图使用的CGContext,只能够再DrawRect:方法中使用,放到别的地方将失效,因为最终调用了这个方法,会将VIew的从新绘制,并将之前的尝试给覆盖了。在别的地方可以进行underlying layer层次的绘图操作~

[self drawPolygon:pointArr];

}

@end

drawRect: 小注,布布扣,bubuko.com

时间: 2025-01-05 01:20:10

drawRect: 小注的相关文章

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

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

关于COCOS2D-X 中的CCRectMake参数小注

在使用CocoStudio和Cocos2d-x混合编程过程中,坐标转换是不可避免的.因此,要随时注意,当然坐标.Rect是全局性的还是局部性的.时间长了,许多基础内容易于忘记,这不---小小的宏函数CCRectMake的参数各自函数就忘记了!故备注于此. 在编码时,当忘记相应参数含义时,我常常按下快捷键F12,转换相应的头文件去分析一下.可怜的是,宏函数CCRectMake各个参数就没有很好的注解. 格式:CCRectMake(x,y,width,height) 对于Widget控件,一般情况下

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篇&amp;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.

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