2

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

在此处输入图像描述

4

3 回答 3

2
[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-05T15:03:50.483 回答
1

为什么不拥有一个带有每个灯光坐标的 CGRect 数组。然后你可以简单地在矩形周围移动 1 个 UIView,或者如果一次照明超过 1 个,然后移动几个 UIView。

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

- (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-05T15:34:46.027 回答
0

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

于 2012-09-05T15:41:16.253 回答