0
我寫了一個函數,每2秒在隨機位置繪製x個正方形。正方形不能在同一個位置上畫兩次。一旦應用程序完成一個循環,網格將被填充。現在,當繪製的正方形數量爲< = 8時,此工作正常,但是當這個高度更高時,正方形會隨機抽取到與之前的正方形相同的位置。setNeedsDisplayInRect(quartz2d,iPhone)問題
而不是繪製一個正方形到隨機的位置我已經嘗試從上到下繪製它們,這完美地工作。
我已經調試了幾天,我相信問題在於setNeedsDisplayInRect。
希望有人能幫忙。很抱歉,如果我有天馬行空,它已經很晚了,我的頭搗碎,哈哈:)
下面是代碼....
- (void)drawRect:(CGRect)rect {
int count = [writeSquares count];
CGContextRef context = UIGraphicsGetCurrentContext();
int radius = 4;
for (int i = 0; i <count; i++)
{
Square *square = [writeSquares objectAtIndex: i];
CGContextSetLineWidth(context, 1);
//CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetFillColorWithColor(context, [square color].CGColor);
CGContextMoveToPoint(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]));
CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMinY([square squareRect]) + radius, radius, 3 * M_PI/2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMaxY([square squareRect]) - radius, radius, 0, M_PI/2, 0);
CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMaxY([square squareRect]) - radius, radius, M_PI/2, M_PI, 0);
CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]) + radius, radius, M_PI, 3 * M_PI/2, 0);
CGContextClosePath(context);
CGContextFillPath(context);
//NSLog(@"x = %f x = %f", CGRectGetMaxX([square squareRect]), CGRectGetMinX([square squareRect]));
}
//NSLog(@"Count %i", [writeSquares count]);
[writeSquares removeAllObjects];
}
-(void)drawInitializer:(int) counter{
if(extra != 0) {
[self setNeedsDisplay];
[grid removeAllObjects];
[self calculateGrid];
counter = counter + extra;
clear = YES;
extra = 0;
}
for(int i=0; i<counter; i++) {
int count = [grid count];
NSLog(@"counter %i", count);
if(count != 144) {
srandom(time(NULL));
int r = random() % [coordinates count];
Coordinate *coordinate = [coordinates objectAtIndex:r];
Square *square = [Square new];
[square setSquareRect: CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];
[square setColor: [UIColor randomColor]];
[writeSquares addObject:square];
[grid addObject:square];
[square release];
[self setNeedsDisplayInRect:CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];
[coordinates removeObjectAtIndex:r];
[coordinate release];
}
else {
extra++;
}
}
//[newlock release];
}
血腥地獄,就是這麼簡單。我花了幾個小時來調試,謝謝你的智慧。 – Carl 2010-01-17 09:51:39