簡體   English   中英

繪制矩形/圓形的Objective-C類

[英]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]; 沒有錯誤。

任何幫助將不勝感激。

您應該閱讀蘋果制圖指南 之后,建議您創建一個CAShapeLayer (或將形狀圖層用作layerClass )。 在圖層上設置UIBezierPath的路徑。 UIBezierPath具有創建橢圓和UIBezierPath便捷方法。)

出現錯誤的原因是因為您在沒有繪圖上下文的viewDidLoad進行繪制。 調用AddRectangle:您試圖繪制,而不僅僅是存儲矩形的坐標。 該函數最好命名為drawBlueRect:或類似的名稱。

要使其正常工作:

  1. 在UIView子類中移動AddRectangle:
  2. 在UIView子類中從drawRect:調用AddRectangle:
  3. 從視圖控制器子類中刪除AddRectangle:

UIRectInset的第二個和第三個參數將導致不繪制矩形,因為它會導致矩形的寬度為零。 您還忽略了AddRectangle:中的rect參數,並且始終相對於self.bounds進行繪制。

您不應該自己調用drawRect: 如果您應該改為調用setNeedsDisplay ,則在調用drawRect: override時將為您配置圖形上下文。 iOS上的最佳做法是使用CALayers而不是drawRect: :。 圖層比drawRect:快得多,尤其是對於動畫而言。

我同意Joride的觀點,您應該閱讀他的建議以及適用於iOSQuartz 2D編程指南 取決於您需要的深度級別,有些時候過時了,David Gelphman和Bunny Laden撰寫的《 在Mac OS X中使用Quartz編程:2D和PDF圖形》是Core Graphics的權威指南。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM