[英]objective-C class to draw rectangle/circle
我是Ios編程的新手。 我想讓類在我的視圖中繪制和管理矩形/圓形。 所以我嘗試從這里使用解決方案。 當我要繪制一個矩形時,它可以正常工作,但是我想有機會繪制和管理更多的矩形。 因此,將其添加到我的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];
沒有錯誤。
任何幫助將不勝感激。
出現錯誤的原因是因為您在沒有繪圖上下文的viewDidLoad
進行繪制。 調用AddRectangle:
您試圖繪制,而不僅僅是存儲矩形的坐標。 該函數最好命名為drawBlueRect:
或類似的名稱。
要使其正常工作:
AddRectangle:
AddRectangle:
AddRectangle:
UIRectInset
的第二個和第三個參數將導致不繪制矩形,因為它會導致矩形的寬度為零。 您還忽略了AddRectangle:中的rect參數,並且始終相對於self.bounds進行繪制。
您不應該自己調用drawRect:
。 如果您應該改為調用setNeedsDisplay
,則在調用drawRect:
override時將為您配置圖形上下文。 iOS上的最佳做法是使用CALayers而不是drawRect:
:。 圖層比drawRect:
快得多,尤其是對於動畫而言。
我同意Joride的觀點,您應該閱讀他的建議以及適用於iOS的Quartz 2D編程指南 。 取決於您需要的深度級別,有些時候過時了,David Gelphman和Bunny Laden撰寫的《 在Mac OS X中使用Quartz編程:2D和PDF圖形》是Core Graphics的權威指南。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.