回到前台时重新启动动画

时间:2016-10-04 06:45:34

标签: ios objective-c animation

所以我正在尝试动画。我有一个UIlabel,我想淡入并在一段时间后淡出。 我在计时器的帮助下成功地完成了它,如第一个答案所示。

Animation done as shown in Answer 1

现在我想在我的应用程序出现前景时重新启动此动画。 问题 :- 我该怎么做才能重新启动动画?

我希望当应用程序进入前台时,UIlabel应该表现得好像这是我第一次开始动画。基本上删除UIlabel上的所有动画并重新开始动画。

5 个答案:

答案 0 :(得分:2)

使用在[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:UIApplicationWillEnterForegroundNotification object:nil]; -(void)myMethod { // start the beiging code here //[self MyLabelAnimation]; [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO]; }

中添加以下方法
(
  // ???
  input CLOCK,
  input RESET,
  input ENABLE,

  // Interconnect
  output [15:0] ADDR,
  ...
);

答案 1 :(得分:1)

你应该这样做

@interface ViewController ()
{
    NSTimer *timer;
}
@end
-(void)viewWillAppear:(BOOL)animated

{
    [super viewWillAppear:animated];
   timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
}
- (void)MyLabelAnimation:(NSTimer*) timer1 {
    self->_mylabel.text = @"Hello";
    [UIView animateWithDuration:0.3 animations:^{
        self->_mylabel.alpha = 1.0;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self->_mylabel.alpha = 0.0;
        } completion:^(BOOL finished) {

            self->_mylabel.text = @"Text 2";
            [UIView animateWithDuration:0.3 animations:^{
                self->_mylabel.alpha = 1.0;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self->_mylabel.alpha = 0.0;
                } completion:^(BOOL finished) {

                    self->_mylabel.text = @"Text 3";
                    [UIView animateWithDuration:0.3 animations:^{
                        self->_mylabel.alpha = 1.0;
                    } completion:^(BOOL finished) {

                        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self->_mylabel.alpha = 0.0;
                        } completion:^(BOOL finished) {

                            self->_mylabel.text = @"Text 4";
                            [UIView animateWithDuration:0.3 animations:^{
                                self->_mylabel.alpha = 1.0;
                            } completion:^(BOOL finished) {
                                [UIView animateWithDuration:0.0 delay:4.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                    self->_mylabel.alpha = 0.0;
                                } completion:^(BOOL finished) {
                                    [self viewWillAppear:YES];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [timer invalidate];
}

答案 2 :(得分:0)

您应该仔细阅读Looking to understand the iOS UIViewController lifecycle

您要使用的方法是

fviewWillAppear

答案 3 :(得分:0)

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
}

// 你的动画....

- (void)MyLabelAnimation:(NSTimer*) timer {
    self->mylabel.text = @"Hello";
    [UIView animateWithDuration:0.3 animations:^{
        self->mylabel.alpha = 1.0;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self->mylabel.alpha = 0.0;
        } completion:^(BOOL finished) {

            self->mylabel.text = @"Text 2";
            [UIView animateWithDuration:0.3 animations:^{
                self->mylabel.alpha = 1.0;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self->mylabel.alpha = 0.0;
                } completion:^(BOOL finished) {

                    self->mylabel.text = @"Text 3";
                    [UIView animateWithDuration:0.3 animations:^{
                        self->mylabel.alpha = 1.0;
                    } completion:^(BOOL finished) {

                        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self->mylabel.alpha = 0.0;
                        } completion:^(BOOL finished) {

                            self->mylabel.text = @"Text 4";
                            [UIView animateWithDuration:0.3 animations:^{
                                self->mylabel.alpha = 1.0;
                            } completion:^(BOOL finished) {
                                [UIView animateWithDuration:0.0 delay:4.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                    self->mylabel.alpha = 0.0;
                                } completion:^(BOOL finished) {

                                     [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

}

答案 4 :(得分:0)

以下是我提出的问题的答案。 Inwit在一个bool变量的帮助下完成它,当应用程序进入后台时设置为No,当进入前景时将设置为yes。

所以下一个动画取决于bool值。

以下是答案的链接 Restart animation after pause