在objective-c中点亮动画

我正在尝试在objective-c中实现一个动画,其中我有一个NY天际线图作为附加图像。 要求是实施城市灯光动画,其中天际线建筑物中的灯光(方形和矩形框)按顺序点亮……以吸引人的动画方式。 实现这一目标的方法之一是使用一组连续图像(UIImageView.animationImages =图像arrays),但我相信可能有更好,更有效的方法来实现这一目标。 如果有人能指出我正确的方向,我将不胜感激。

在此处输入图像描述

为什么没有一个CGRect的数组与每个灯的坐标。 然后你可以简单地移动1个UIView围绕rects,或者如果一次点亮1个以上,那么移动几个UIViews。

编辑——-只是将一些快速代码放在一起。

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; rects = [[NSMutableArray alloc] init]; lit = [[NSMutableArray alloc] init]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(20, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(60, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(100, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(140, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(180, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(220, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(260, 20, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(20, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(60, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(100, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(140, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(180, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(220, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(260, 60, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(20, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(60, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(100, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(140, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(180, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(220, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(260, 100, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(20, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(60, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(100, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(140, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(180, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(220, 140, 20, 20)]]; [rects addObject:[NSValue valueWithCGRect:CGRectMake(260, 140, 20, 20)]]; } - (void)viewDidAppear:(BOOL)animated { [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 yellowColor]; [lit addObject:light]; [self.view addSubview:light]; escape = YES; } } [self performSelector:@selector(lightRandomLight) withObject:nil afterDelay:0.2]; } - (void)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]; } 
 [UIView animateWithDuration:1 delay:0 options:0 animations:^{ light1.alpha = 1; } completion:nil]; [UIView animateWithDuration:1 delay:0.5 options:0 animations:^{ light2.alpha = 1; } completion:nil]; [UIView animateWithDuration:1 delay:1 options:0 animations:^{ light3.alpha = 1; } completion:nil]; [UIView animateWithDuration:1 delay:1.5 options:0 animations:^{ light4.alpha = 1; } completion:nil]; 

在创建它们时将灯光视图alpha设置为0,并在-viewDidAppear运行它可能……(这假设每个灯光是一个单独的UIView ……)

如果你有很多观点,你可以这样做来动画每个人的alpha值:

 float n = 0; for (UIView* v in self.view.subviews) { if (v.alpha < 0.5) { [UIView animateWithDuration:1 delay:n options:0 animations:^{ v.alpha = 1; } completion:nil]; n += 0.5; } } 

这可能有点工作,但我可能会尝试将窗口颜色设置为alpha,然后“翻转”图像后面的灯光。