使用三个图像的IOS动画

时间:2014-08-19 20:58:51

标签: ios objective-c animation uiimage

我有一只睁着眼睛的豹的图像,两只PNG的透明背景,同一只黑豹眼睛中间闭合和闭合。我试图让它动画似乎每隔30秒就会闪烁一次,但我无法弄清楚如何让它正常工作,甚至在动画完成后30秒后再重复。

到目前为止,这是我的代码:

//This is the panther image, eyes are open
UIImageView *PantherOpenImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Screen_01_PantherEyes_Open"]];
PantherOpenImageView.frame = self.view.bounds;
[self.view addSubview:PantherOpenImageView];
PantherOpenImageView.layer.zPosition = 1;

//This is the png image of the panthers eyes, eyes mid-closed
UIImageView *PantherMidImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Screen_01_PantherEyes_Mid"]];
PantherMidImageView.frame = self.view.bounds;
[self.view addSubview:PantherMidImageView];
PantherMidImageView.layer.zPosition = 2;
PantherMidImageView.alpha = 0; 

//This is the png image of the panthers eyes, eyes closed
UIImageView *PantherClosedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Screen_01_PantherEyes_Closed"]];
PantherClosedImageView.frame = self.view.bounds;
[self.view addSubview:PantherClosedImageView];
PantherClosedImageView.layer.zPosition = 2;
PantherClosedImageView.alpha = 0;

[UIView animateWithDuration:0.7f
          delay:4.0f
          options:UIViewAnimationOptionBeginFromCurrentState
          animations:^{
                    PantherMidImageView.alpha = 1;
                    //delay
                    PantherMidImageView.alpha = 0;
                    PantherClosedImageView.alpha = 1;
                    //delay
                    PantherClosedImageView.alpha = 0;
                 }
         completion:NULL];

1 个答案:

答案 0 :(得分:0)

animations:^{}块中的所有内容同时执行,因此您需要定义具有不同延迟的四个动画。我建议将闭眼层的zPosition设置为3,这样你就可以定义一个"闪烁"像这样的方法:

- (void)pantherBlink
{
    [UIView animateWithDuration:0.7f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         PantherMidImageView.alpha = 1;
                     }
                     completion:NULL];

    [UIView animateWithDuration:0.7f
                          delay:0.7f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         PantherClosedImageView.alpha = 1;
                     }
                     completion:NULL];

    [UIView animateWithDuration:0.7f
                          delay:1.4f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         PantherClosedImageView.alpha = 0;
                     }
                     completion:NULL];

    [UIView animateWithDuration:0.7f
                          delay:2.1f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         PantherMidImageView.alpha = 0;
                     }
                     completion:NULL];
}

然后,您可以创建一个每30秒调用此方法的计时器:

NSTimer blinkTimer = [NSTimer scheduledTimerWithTimeInterval:30.0
                                                      target:self
                                                    selector:@selector(pantherBlink)
                                                    userInfo:nil
                                                     repeats:YES];