UIView Animation wont Delay!


December 2018


774 time


I have a simple UIView backgroundColor transition running on its own thread as such:

- (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;

however... when It runs after initializing with such:

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

the colors never change over time, the animation runs through as if there is no delay at all and the background changes so rapidly the AppReviewers would reject my app under articles responsible for epileptic seizures. Please someone help me understand why the animation never carries out over time and instead flash finishes!

1 answers


Попробуйте self.layer.backgroundColor = [[self randomColor] CGColor].