Ios 如何在迭代中利用视图,正如我将setNeedsDisplay方法放入无限while循环中一样
我是iOS新手,在循环中在视图上绘图有问题,这是我在MyView.m类中的drawRect方法: ` ` MyView作为xib中的子视图添加到ViewController中 这是viewController中的my while循环。m:Ios 如何在迭代中利用视图,正如我将setNeedsDisplay方法放入无限while循环中一样,ios,uiview,setneedsdisplay,Ios,Uiview,Setneedsdisplay,我是iOS新手,在循环中在视图上绘图有问题,这是我在MyView.m类中的drawRect方法: ` ` MyView作为xib中的子视图添加到ViewController中 这是viewController中的my while循环。m: -(void)buttonClicked:(id)sender { while (TRUE) { NSLog(@"while"); NSThread* myThread = [[NSThread alloc]
-(void)buttonClicked:(id)sender
{
while (TRUE) {
NSLog(@"while");
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(reDraw)object:nil];
[myThread start];
// [NSThread detachNewThreadSelector:@selector(reDraw) toTarget:self withObject:nil];
//[self performSelector:@selector(reDraw) withObject:nil afterDelay:1];
NSLog(@"after sleep");
}
}
-(void)reDraw {
[myView setNeedsDisplay];
}
buttonClicked是当我按下按钮开始时调用的方法,而循环迭代setNeedsDisplay方法,问题是当我按下按钮时,一切都停止工作并接受图形。按钮保持单击状态,所有其他组件停止。当您按下按钮时,将进入主线程上的无限循环。主线程是绘制所有图形的线程。由于
setNeedsDisplay
的原因,绘图并不是直接完成的,尽管如此,您所做的只是将大量的绘图请求排队,然后停止主线程,这样就永远无法完成
基本上,你永远不应该睡在主线上,永远,不管什么原因,永远
您应该考虑使用
NSTimer
调用您的reDraw
方法,因为它将以您设置的速率运行,并且不会阻塞主线程。感谢您的帮助,我会尝试在之后通知您。如果您是iOS新手,为什么要尝试使用NSThread方法?按下按钮:方法很可怕。
-(void)buttonClicked:(id)sender
{
while (TRUE) {
NSLog(@"while");
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(reDraw)object:nil];
[myThread start];
// [NSThread detachNewThreadSelector:@selector(reDraw) toTarget:self withObject:nil];
//[self performSelector:@selector(reDraw) withObject:nil afterDelay:1];
NSLog(@"after sleep");
}
}
-(void)reDraw {
[myView setNeedsDisplay];
}