首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >objective-c中的灯光动画

objective-c中的灯光动画
EN

Stack Overflow用户
提问于 2012-09-05 22:56:00
回答 3查看 788关注 0票数 2

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-05 23:34:46

为什么不用CGRect数组来表示每个灯光的坐标。然后你可以简单地在长方形周围移动1个UIView,或者如果一次照明超过1个,那么就移动几个UIViews。

编辑-只是一些快速的代码拼凑在一起。

代码语言:javascript
复制
- (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];
}
票数 1
EN

Stack Overflow用户

发布于 2012-09-05 23:03:50

代码语言:javascript
复制
[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值具有动画效果:

代码语言:javascript
复制
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;
    } 
}
票数 2
EN

Stack Overflow用户

发布于 2012-09-05 23:41:16

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12284250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档