2012-04-28 76 views
1

我想在iphone sdk中繪製像矩陣結構的點。在iphone上繪製像矩陣結構的點sdk

,如果我想生成點列和行(如2x2,3x3,4x4)。

但我不知道這一代。

我想輸出像下面的圖像。

enter image description here

例如代碼或教程任何幫助..!

+0

在CG,點很簡單,但虛線很貴。你試圖完成哪一個。 – CodaFi 2012-04-28 05:58:18

+0

如果你能更詳細地闡述這一點會更好。爲了簡單起見,您只需使用數組就可以實現此目的。 – Tarun 2012-04-28 06:00:20

回答

4

您使用此代碼...

- (void)drawRect:(CGRect)rect 
{ 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 

CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1); 
//CGContextSetRGBFillColor(contextRef, 0, 10, 200, 0.2); 

CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5); 

// Draw a circle (filled) 
//CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 

// Draw a circle (border only) 
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 



// Get the graphics context and clear it 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextClearRect(ctx, rect); 



// Draw a green solid circle 
CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(20, 50, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(50, 50, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(80, 50, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(110, 50, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(140, 50, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(170, 50, 8, 8)); 



CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(20, 100, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(50, 100, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(80, 100, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(110, 100, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(140, 100, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(170, 100, 8, 8)); 


CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(20, 150, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(50, 150, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(80, 150, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(110, 150, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(140, 150, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(170, 150, 8, 8)); 


CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(20, 200, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(50, 200, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(80, 200, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(110, 200, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(140, 200, 8, 8)); 

CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
CGContextFillEllipseInRect(ctx, CGRectMake(170, 200, 8, 8)); 
} 

你的輸出顯示像 enter image description here

+0

+ 1,感謝您的代碼...! – Dinesh 2012-04-28 06:12:46

+2

如果你喜歡這個代碼,接受這個並增加你的積分。 – akk 2012-04-28 06:24:53

+0

硬編碼值?我喜歡它,+1! – JustSid 2012-04-28 06:29:46

6

一個簡單的解決辦法是:

- (void)drawRect:(CGRect)rect { 
    int n = 12;   // row and column count 
    int padding = 5; // distance beetween dots 
    float radius = MIN((rect.size.width-(n+1)*padding)/(n*2), (rect.size.height-(n+1)*padding)/(n*2)); // radius depending on rect size 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); // background color 
    CGContextFillRect(context, rect); // setting background color 

    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); // dot color 

    for (int y = 0; y<n; y++) { 
    for (int x = 0; x<n; x++) { 
     CGContextAddArc(context, padding+radius+(padding+radius*2)*x, padding+radius+(padding+radius*2)*y, radius, 0, M_PI*2, 0); // adding dot path 
     CGContextFillPath(context); // drawing dot path 
    } 
    } 
} 
+0

如何調用此方法..! – Dinesh 2012-04-28 06:30:47

+0

您簡單的必須執行的UIView的子類,覆蓋這個實現drawRect方法。 – 2012-04-28 06:37:40

+0

我打電話給這個方法顯示錯誤:[self drawRect:my_uiview]; – Dinesh 2012-04-28 06:42:28