UIView动画不会延迟!

时间:2010-02-28 19:19:23

标签: animation uiview colors delay

我有一个简单的UIView backgroundColor转换在它自己的线程上运行:

- (void)colorLoop_thread {  
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// [self performSelector:@selector(changeColors)];
[self performSelectorOnMainThread:@selector(changeColors) withObject:nil waitUntilDone:NO];

 [pool release];
}

- (void)changeColors {
 if(self.colorStatus == colorBegin) {
  self.backgroundColor = [self randomColor];
  self.colorStatus = colorChanged;
 }

 if(self.colorStatus == colorChanged) {
  self.colorStatus = colorChanging;
  [UIView beginAnimations:@"ChangeColor" context:nil];
  [UIView setAnimationDuration:2.0f];
  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(changeColorFinished:finished:context:)];
  self.backgroundColor = [self randomColor];
  [UIView commitAnimations];
 }
 [NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(changeColors) userInfo:nil repeats:NO];
}

- (void)changeColorFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
 if([animationID isEqualToString:@"ChangeColor"]) {
  self.colorStatus = colorChanged;
 }
}

然而......当它用以下内容初始化后运行时

colorStatus = colorBegin;
static BOOL colorThread = NO;if(!colorThread) {colorThread = YES;[NSThread detachNewThreadSelector:@selector(colorLoop_thread) toTarget:self withObject:nil];}

颜色永远不会随着时间的推移而变化,动画会一直运行,好像根本没有延迟,而且背景变化如此之快,AppReviewers会根据负责癫痫发作的文章拒绝我的应用程序。请有人帮助我理解为什么动画永远不会随着时间的推移而闪光完成!

1 个答案:

答案 0 :(得分:0)

尝试self.layer.backgroundColor = [[self randomColor] CGColor]