objective-C类绘制矩形/圆

时间:2014-07-11 11:47:54

标签: ios drawing core-graphics

我是Ios编程的新手。我想让课程在我的视图中绘制和管理矩形/圆形。所以我尝试使用here的解决方案。当我想绘制一个Rectangle时,它工作正常,但我希望有机会绘制和管理更多的矩形。 所以我将其添加到我的CircleManager.h文件中:

-(void)AddRectangle:(CGRect) rect;
{
//[self drawRect:rect];
[[UIColor blueColor] setFill];
UIRectFill(CGRectInset(self.bounds, 150, 150));
}

在我的ViewControler.m中添加此代码:

- (void)viewDidLoad {  
CircelManager* view = [[CircelManager alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];  
view.backgroundColor = [UIColor blackColor];
[view AddRectangle:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:view];
[super viewDidLoad];   
}

当我尝试运行此代码时,我在输出中得到了这个:

Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730] <Error>: CGContextGetCompositeOperation: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730] <Error>: CGContextSetCompositeOperation: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730] <Error>: CGContextFillRects: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730] <Error>: CGContextSetCompositeOperation: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

我注意到如果我评论第[self.view addSubview:view];行,则没有错误。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您应该阅读drawing guide by apple。之后,我建议您创建一个CAShapeLayer(或使用形状图层为layerClass的视图)。在图层上设置UIBezierPath的路径。 (UIBezierPath有方便的方法来创建椭圆和rects。)。

答案 1 :(得分:0)

您收到错误的原因是您正在绘制viewDidLoad,它没有绘图上下文。当您试图绘制AddRectangle:时,您正在尝试绘制,而不仅仅是存储矩形的坐标。该函数更好地命名为drawBlueRect:或类似的东西。

要使其发挥作用:

  1. 在您的UIView子类中移动AddRectangle:
  2. 从您的UIView子类
  3. 中的drawRect:调用AddRectangle:
  4. 从视图控制器子类中删除AddRectangle:
  5. UIRectInset的第二个和第三个参数将导致不绘制矩形,因为它会生成宽度为零的矩形。你也忽略了AddRectangle中的rect参数:并且总是相对于self.bounds进行绘制。

    你不应该自己打电话给drawRect:。如果您应该调用setNeedsDisplay,则在调用drawRect:覆盖时将为您配置图形上下文。 iOS上的最佳做法是使用CALayers而不是drawRect:。图层比drawRect:快得多,特别是对于动画。

    我同意Joride的意见,你应该阅读他所建议的内容以及Quartz 2D Programming Guide for iOS。根据您需要的深度,有点过时,David Gelphman和Bunny Laden的Programming with Quartz: 2D and PDF Graphics in Mac OS X是Core Graphics的权威指南。