我试图通过拥有一个 CGRect 数组和每个灯光的坐标来实现城市灯光动画。然后围绕这些 CGRects 创建 UIViews。这个逻辑(感谢Darren帮助这个逻辑)工作正常,但是在动画完成后,屏幕上的所有其他元素(UIButtons、Sliders、其他 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];
}