0

So i was trying my hands on animation. I have a UIlabel which i want to fade in on and fade out after certain period. I successfully done it as shown in the first answer with the help of timer.

Animation done as shown in Answer 1

Now i want to restart this animation when my app comes foreground. Problems :- What should i do to restart animations?

I want that when app comes in foreground, UIlabel should act as if this is the first time i am starting animation. Basically remove all animation on UIlabel and do start animation fresh.

Community
  • 1
  • 1
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74
  • i have already give you answer in your first question check it – Himanshu Moradiya Oct 04 '16 at 06:46
  • @HimanshuMoradiya When coming in foreground i am handling it with notifications. so i tried to again start it with [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(dooratepLabelAnimation:) userInfo:nil repeats:NO]; but uilabel starts flickering. – Sudhanshu Gupta Oct 04 '16 at 06:51
  • i will give you full code work with Notification .. – Himanshu Moradiya Oct 04 '16 at 06:58
  • when u run app first time , Animation is normal or flickering ? – Jitendra Modi Oct 04 '16 at 07:13
  • at first animation is perfect, then i tap home button, then i tap on app icon, app comes back to foreground , flickering starts in uilabel – Sudhanshu Gupta Oct 04 '16 at 07:17
  • You just have to call timer method in viewwill appear. It seems that Everytime this method calls and your method calls. Use viewWillappear for that – Jitendra Modi Oct 04 '16 at 07:23
  • @Jecky can you also tell me why because it seems when home button is pressed viewillappear is not getting called. Then say i present a viewcontroller then while dismissing that , viewillappear of myviewcontroller is called amd flickering starts again – Sudhanshu Gupta Oct 04 '16 at 09:11
  • @SudhanshuGupta Check my answer below. I have tested this in my demo application. If work then vote , if it is not worked then Surely downvote it – Jitendra Modi Oct 04 '16 at 10:26

5 Answers5

2

use like add the following method in your ViewDidLoad

[[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];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

You should do like this

@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];
}
Jitendra Modi
  • 2,344
  • 12
  • 34
0

You should read Looking to understand the iOS UIViewController lifecycle carefully.

The methods your are looking to use are

viewWillAppear and viewWillDisappear

Community
  • 1
  • 1
Antzi
  • 12,831
  • 7
  • 48
  • 74
0
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
}

// Your animation....

- (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];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

Here is the answer for the question I have asked. Inwit done it with the help of a bool variable which sets to No when app goes in background and will get set to yes when goes in foreground.

SO the next animation depends on the bool value.

Here is the link for the answer Restart animation after pause

Community
  • 1
  • 1
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74