我每两秒从 NSTimer 函数调用 setNeedsDisplayInRect 。这完美地工作,并在随机位置绘制一个正方形,具有随机颜色。但是,在某些情况下,我想在 NSTimer 函数中绘制平方 x 次(使用 for 循环) - 但是,经过多次错误测试后,即使我正在运行 setNeedsDisplayInRect x number of次?我希望得到一些帮助,因为我整天都在试图解决这个问题。卡尔。
下面编辑是我的代码...
看法
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextSetFillColorWithColor(context, currentColor.CGColor);
CGContextAddRect(context, redrawRect);
CGContextDrawPath(context, kCGPathFillStroke);
}
-(void)drawInitializer
{
int x = (int)arc4random() % [self.xCoordinates count];
int y = (int)arc4random() % [self.yCoordinates count];
self.currentColor = [UIColor randomColor];
self.redrawRect = CGRectMake ([[self.xCoordinates objectAtIndex:x] intValue], [[self.yCoordinates objectAtIndex:y] intValue], 25, 25);
[self setNeedsDisplayInRect:redrawRect];
}
控制器
- (void) handleTimer: (NSTimer *) timer
{
for(int i=0; i<5; i++)
{
[self.squareView drawInitializer];
}
}