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

发布于 2012-09-05 23:34:46
为什么不用CGRect数组来表示每个灯光的坐标。然后你可以简单地在长方形周围移动1个UIView,或者如果一次照明超过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];
}发布于 2012-09-05 23:03:50
[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;
}
}发布于 2012-09-05 23:41:16
这可能有点麻烦,但我可能会尝试将窗口颜色设置为alpha,然后“翻转”图像后面的灯光。
https://stackoverflow.com/questions/12284250
复制相似问题