Ios objective-C类绘制矩形/圆
我是Ios编程新手。我想让学生在我的视图中绘制和管理矩形/圆形。因此,我尝试使用来自的解决方案。当我想画一个矩形时,它工作得很好,但我想有机会画和管理更多的矩形。 因此,我将其添加到我的Ios objective-C类绘制矩形/圆,ios,drawing,core-graphics,Ios,Drawing,Core Graphics,我是Ios编程新手。我想让学生在我的视图中绘制和管理矩形/圆形。因此,我尝试使用来自的解决方案。当我想画一个矩形时,它工作得很好,但我想有机会画和管理更多的矩形。 因此,我将其添加到我的CircleManager.h文件中: -(void)AddRectangle:(CGRect) rect; { //[self drawRect:rect]; [[UIColor blueColor] setFill]; UIRectFill(CGRectInset(self.bounds, 150, 150)
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.
Jul 11 13:37:38 SG-MacBook-Air.local NavTry1[1730]:CGContextSetFillColorWithColor:无效上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
7月11日13:37:38 SG-MacBook-Air.local NavTry1[1730]:CGContextGetCompositeOperation:无效上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
7月11日13:37:38 SG-MacBook-Air.local NavTry1[1730]:CGContextSetCompositeOperation:无效上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
7月11日13:37:38 SG-MacBook-Air.local NavTry1[1730]:CGContextFillRects:无效上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
7月11日13:37:38 SG-MacBook-Air.local NavTry1[1730]:CGContextSetCompositeOperation:无效上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
我注意到,如果我注释行[self.view addSubview:view]代码>没有错误
任何帮助都将不胜感激。您应该阅读。之后,我建议您创建一个CAShapeLayer
(或者一个将形状层用作layerClass
)的视图)。在图层上设置一个UIBezierPath
的路径。(UIBezierPath
提供了创建椭圆和矩形的方便方法。)。出现错误的原因是您在没有绘图上下文的viewDidLoad
中绘图。调用AddRectangle:
时,您正在尝试绘制,而不仅仅是存储矩形的坐标。该函数最好命名为drawBlueRect:
或类似的名称
要使其正常工作,请执行以下操作:
在UIView子类中移动AddRectangle:
从UIView子类中的drawRect:调用AddRectangle:
从视图控制器子类中删除AddRectangle:
UIRectInset
的第二个和第三个参数将导致不绘制矩形,因为它会生成宽度为零的矩形。您还将忽略AddRectangle:中的rect参数,并始终相对于self.bounds绘制
您不应该自己调用drawRect:
。如果您应该调用setNeedsDisplay
,则在调用drawRect:
覆盖时将为您配置图形上下文。iOS上的最佳实践是使用CALayers而不是drawRect:
。层比drawRect:
快得多,尤其是对于动画
我同意乔里德的意见,即你应该阅读他的建议以及报告。David Gelphman和Bunny Laden的著作虽然有点过时,但根据您需要的深度级别,它是核心图形的最终指南。可能的副本