使用三个图像的IOS动画

问题描述 投票:0回答:1

我有一只睁着眼睛的豹的图像,两只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];
ios objective-c animation uiimage
1个回答
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];
© www.soinside.com 2019 - 2024. All rights reserved.