【问题标题】:objective-C class to draw rectangle/circle用于绘制矩形/圆形的 Objective-C 类
【发布时间】:2014-07-11 11:47:53
【问题描述】:

我是 iOS 编程新手。我想上课来在我的视图中绘制和管理矩形/圆形。所以我尝试使用here 的解决方案。当我想绘制一个矩形时它工作得很好,但我想有机会绘制和管理更多的矩形。 所以我将此添加到我的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]; 没有错误。

任何帮助将不胜感激。

【问题讨论】:

标签: ios drawing core-graphics


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    要让它工作:

    1. 在你的 UIView 子类中移动AddRectangle:
    2. 从 drawRect 调用AddRectangle::在你的 UIView 子类中
    3. 从您的视图控制器子类中删除 AddRectangle:

    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 的权威指南。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2014-10-23
      相关资源
      最近更新 更多