动画完成后,所有UIElements都无法访问

我正在尝试通过使用每个灯光的坐标的CGRect数组来实现城市灯光动画。 然后围绕这些CGRects创建UIViews。 这个逻辑(感谢Darren帮助这个逻辑)工作正常但是在动画完成后,屏幕上的所有其他元素(UIButtons,Sliders,Other UIImageViews等)变得无法访问。 甚至我的滑动手势都没有响应。 在动画之前和期间,所有元素都响应良好但是一旦动画完成,它们都变得无法访问。 我还尝试了[UIView bringSubviewToFront:]将一些元素放在前面,看看是否有助于使它们可访问,但它没有帮助。 我认为这不是问题,因为即使我尝试通过将它们发送到后台来创建灯光视图[self.view sendSubviewToBack:light]; 动画完成后,一切都变得无法访问。

如果有人可以帮助/建议我缺少什么,我将不胜感激。

这是我的代码逻辑和城市灯光动画的相应场景。 rects和lits是ivars。

在此处输入图像描述

 - (void)viewDidLoad { pageCount=5; AVAudioSession * audioSession = [AVAudioSession sharedInstance]; //Setup the audio session //... pageNum=1; //put imageviews in place //... //load a page [self loadPage]; [self loadAudio]; [self cityLightsSetUp]; [super viewDidLoad]; } -(void)loadPage{ //Logic to load page... /... if (pageNum == 3){ [self startCityLights]; } } - (void)cityLightsSetUp { rects = [[NSMutableArray alloc] init]; lit = [[NSMutableArray alloc] init]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 217, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 217, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 217, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 217, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 231, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 231, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 231, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 231, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 245, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 245, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(84, 245, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(97, 245, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(58, 258, 10, 10)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(71, 258, 10, 10)]]; } - (void)startCityLights { [self lightRandomLight]; [self performSelector:@selector(switchOffRandomLight) withObject:nil afterDelay:1.0]; } - (void)lightRandomLight { BOOL escape = NO; int rand; while (!escape) { BOOL alreadyLit = NO; rand = arc4random() % [rects count]; // Check if already lit for (UIView *view in lit) { CGRect litRect = view.frame; CGRect ranRect = [[rects objectAtIndex:rand] CGRectValue]; if (CGRectContainsRect(litRect, ranRect)) { alreadyLit = YES; } } if (!alreadyLit) { UIView *light = [[UIView alloc] initWithFrame:[[rects objectAtIndex:rand] CGRectValue]]; light.backgroundColor = [UIColor orangeColor]; [lit addObject:light]; [self.view addSubview:light]; //[self.view sendSubviewToBack:light]; escape = YES; } } [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2]; } - (void)switchOffRandomLight { NSLog(@"switchOffRandomLight"); int rand = arc4random() % [lit count]; UIView *light = [lit objectAtIndex:rand]; [lit removeObject:light]; [light removeFromSuperview]; [self performSelector:@selector(switchOffRandomLight) withObject:nil afterDelay:0.5]; } 

看来,一旦所有灯点亮,while循环就会连续运行,试图获得一个未点亮的随机数。 这将阻止主线程。

您只需检查方法开头是否所有灯都点亮,如果没有则继续。

在lightRandomLight方法的底部,替换

 [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2]; 

 if ([lit count] != [rects count]) { [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2]; } else { [NSObject cancelPreviousPerformRequestsWithTarget:self]; } 

当所有灯都亮起时它会停止。

你正在阻塞主线程 – lightRandomLight在完成时调用自身,延迟时间为0.2秒。 因此,主线程永远不会响应用户交互。 查看UIView提供的动画API – 而不是通过添加和删除子视图来“动画化”,您可以设置视图的alpha状态或背景颜色,并且可以设置延迟和设置循环。

我的猜测是, while (!escape) {}永远不会完成(所以逃避永远不会是)。