iosios uiview drawrectt什么时候调用

iOS开发(93)
原文链接:http://blog.csdn.net/nyh1006/article/details/
iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。
下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:
- (void)drawRect:(CGRect)rect { &
&&&&// Drawing code. &
&&&&//获得处理的上下文 &&&
&&&&CGContextRef context = UIGraphicsGetCurrentContext(); &&&
&&&&//设置线条样式 &&&
&&&&CGContextSetLineCap(context, kCGLineCapSquare); &&&&
&&&&//设置线条粗细宽度 &&&
&&&&CGContextSetLineWidth(context, 1.0); &&&&
&&&&//设置颜色 &&&
&&&&CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); &&&&
&&&&//开始一个起始路径 &&&
&&&&CGContextBeginPath(context); &&&&
&&&&//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标, &&&
&&&&CGContextMoveToPoint(context, 0, 0); &&&&
&&&&//设置下一个坐标点 &&&
&&&&CGContextAddLineToPoint(context, 100, 100); &&&&
&&&&//设置下一个坐标点 &&&
&&&&CGContextAddLineToPoint(context, 0, 150); &&&
&&&&//设置下一个坐标点 &&&
&&&&CGContextAddLineToPoint(context, 50, 180); &&&
&&&&//连接上面定义的坐标点 &&&
&&&&CGContextStrokePath(context); &
再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)
在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次.&当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.
drawRect调是在Controller-&loadView, Controller-&viewDidLoad 两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).
1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。
2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
setNeedsLayout可以重新调用drawRect:方法,实现重绘的功能;
- (void)drawRect:(CGRect)
- (void)setNeedsD
- (void)setNeedsDisplayInRect:(CGRect)
一些常见问题
问1:由UIImageView派生出的类,为什么在drawRect中,无法绘制矩形?但如果从UIView派出就可以?
答:苹果的官方文档说了, UIImageView是专门为显示图片做的控件,用了最优显示技术,是不让调用darwrect方法, 要调用这个方法,只能从uiview里重写。
问2:在ios开发中 ,为什么自定义UIView 重写drawRect方法之后,绘图区域之外为黑,合理的情况是,我没有绘制或者填充的区域应该是透明的才对啊,如果我希望没有绘制的地方为 透明 该如何做?
答:self.backgroundColor = [UIColor clearColor]; 并记得 [super drawRect];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:90592次
积分:1388
积分:1388
排名:千里之外
原创:28篇
转载:132篇
(27)(5)(5)(10)(5)(3)(5)(3)(14)(14)(7)(10)(18)(22)(6)(4)ios 画图必须在drawrect里执行吗_百度知道114网址导航09:55 提问
iphone使用drawRect画圆
我要在UIView中画一个简单的圆形。我不想用QuartzCore,能不能用drawRect实现?
- (void)drawRect:(CGRect)rect
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, rect);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor greenColor] CGColor]));
CGContextFillPath(ctx);
上述代码在设置[UIColor whiteColor]时就不工作。在设置了[UIColor whiteColor],视图就变成了透明的。
为什么设置白色但是视图变透明?怎么样画圈?
按赞数排序
greenColor 来自RGB颜色,但是whiteColor不是,所以你用错了。
最好的方法是去掉: CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor greenColor] CGColor]));
[[UIColor greenColor] set];
[[UIColor whiteColor] set];
另外一点,如果你需要Core Graphics,代码如下:
CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
190关注|281收录
302关注|1051收录
1268关注|732收录
其他相似问题[ios]调整大小 UIImage drawRect 用黑色填充背景
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我有一个名为 MNTRectangle 的是 UIImage 的一个子类。我已经重写 drawRect 方法的此类以 (使用其框架) 在图像上绘制一个边框。我有它,所以当用户开始拖动平移的手势叫查看 (UIView 的子类) 的类上它添加的一个实例 MNTRectangle 作为子视图的查看实例。随着用户继续拖动调整大小,MNTRectangle。问题是 MNTRectangle 出现作为固体黑在结束时,我试过清除图形上下文以及保存之前绘制边框和绘图边框后恢复上下文上下文。无论我似乎试我无法 MNTRectangle 清除并只显示上的边框调整大小。
这里是我的 MNTRectangle 类的代码:
@implementation MNTRectangle
- (id)init
self = [super init];
[self setup];
- (id)initWithCoder:(NSCoder *)aDecoder
self = [super initWithCoder:aDecoder];
[self setup];
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
[self setup];
- (void)setup
[self setBackgroundColor:[UIColor clearColor]];
- (void)drawRect:(CGRect)rect
// Get graphics context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextClearRect(context, rect);
// Draw border
CGContextSetLineWidth(context, 4.0);
[[UIColor blackColor] setStroke];
CGContextStrokeRect(context, rect);
CGContextRestoreGState(context);
这里是代码中查看在 UIView 上平移拖动处理:
-(id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
- (void)handlePan:(UIPanGestureRecognizer *)sender
if ([sender state] == UIGestureRecognizerStateBegan)
_startPanPoint = [sender locationInView:self];
MNTRectangle *rectangle = [[MNTRectangle alloc] initWithFrame:CGRectMake(_startPanPoint.x, _startPanPoint.y, 1, 1)];
[_objects addObject:rectangle];
_currentPanObject =
[self addSubview:rectangle];
else if ([sender state] == UIGestureRecognizerStateChanged)
CGPoint endPanPoint = [sender locationInView:self];
float height = fabsf(endPanPoint.y - _startPanPoint.y);
float width = fabsf(endPanPoint.x - _startPanPoint.x);
float x = MIN(_startPanPoint.x, endPanPoint.x);
float y = MIN(_startPanPoint.y, endPanPoint.y);
MNTRectangle *rectangle = (MNTRectangle *)_currentPanO
[rectangle setFrame:CGRectMake(x, y, width, height)];
else if ([sender state] == UIGestureRecognizerStateEnded)
CGPoint endPanPoint = [sender locationInView:self];
float height = fabsf(endPanPoint.y - _startPanPoint.y);
float width = fabsf(endPanPoint.x - _startPanPoint.x);
float x = MIN(_startPanPoint.x, endPanPoint.x);
float y = MIN(_startPanPoint.y, endPanPoint.y);
MNTRectangle *rectangle = (MNTRectangle *)_currentPanO
[rectangle setFrame:CGRectMake(x, y, width, height)];
任何与此的帮助非常感谢。
解决方法 1:
我终于弄清楚。在我 handlePan: 方法设置矩形的帧后我错失了 [rectangle setNeedsDisplay]; 。
现在我的查看代码如下所示:
-(id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
- (void)handlePan:(UIPanGestureRecognizer *)sender
if ([sender state] == UIGestureRecognizerStateBegan)
_startPanPoint = [sender locationInView:self];
MNTRectangle *rectangle = [[MNTRectangle alloc] initWithFrame:CGRectMake(_startPanPoint.x, _startPanPoint.y, 1, 1)];
[_objects addObject:rectangle];
_currentPanObject =
[self addSubview:rectangle];
else if ([sender state] == UIGestureRecognizerStateChanged)
CGPoint endPanPoint = [sender locationInView:self];
float height = fabsf(endPanPoint.y - _startPanPoint.y);
float width = fabsf(endPanPoint.x - _startPanPoint.x);
float x = MIN(_startPanPoint.x, endPanPoint.x);
float y = MIN(_startPanPoint.y, endPanPoint.y);
MNTRectangle *rectangle = (MNTRectangle *)_currentPanO
[rectangle setFrame:CGRectMake(x, y, width, height)];
[rectangle setNeedsDisplay];
else if ([sender state] == UIGestureRecognizerStateEnded)
CGPoint endPanPoint = [sender locationInView:self];
float height = fabsf(endPanPoint.y - _startPanPoint.y);
float width = fabsf(endPanPoint.x - _startPanPoint.x);
float x = MIN(_startPanPoint.x, endPanPoint.x);
float y = MIN(_startPanPoint.y, endPanPoint.y);
MNTRectangle *rectangle = (MNTRectangle *)_currentPanO
[rectangle setFrame:CGRectMake(x, y, width, height)];
[rectangle setNeedsDisplay];}

我要回帖

更多关于 ios drawrect详解 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信