Restart animation when comes back in foreground - ios

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.

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

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

You should read Looking to understand the iOS UIViewController lifecycle carefully.
The methods your are looking to use are
viewWillAppear and viewWillDisappear

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

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

Related

Animating Labels with delay and duration Ios

So i was trying to animate label which will start with a text then after certain time it will fade and new text comes to the screen which will fade after certain time and then new text comes. I want this to happen recursively. I need to continue animating for 15 seconds , the again same thing happens for next 15 seconds.
- (void)viewDidLoad {
[super viewDidLoad];
[self MyLabelAnimation];
}
- (void)MyLabelAnimation {
self.myLabel.text = #"Text 1";
[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 MyLabelAnimation];
}];
}];
}];
}];
}];
}];
}];
}];
}
So Here i am calling mylabel animation from viewdidload and then i called the method at the end of the completion block of 4th animation.
Here everything is working as expected but the last animation to first animation transition is not smooth as other transition. Is it because i missed something . I really am not able to figure out why is it happening.
Secondly, Is is the right way to get the animation like this. Or is there a better way to do this ?
You might find something like this a bit easier to manage:
- (void)viewDidLoad {
[super viewDidLoad];
self.counter = 0;
self.animations = #[
#{#"Text":#"Hello", #"Duration":#(2.7)},
#{#"Text":#"Text 1", #"Duration":#(2.7)},
#{#"Text":#"Text 2", #"Duration":#(2.7)},
#{#"Text":#"Text 3", #"Duration":#(2.7)},
#{#"Text":#"Text 4", #"Duration":#(4.8)},
];
[self animate:0];
}
- (void)animate:(int)counter {
self.counter = counter % self.animations.count;
NSDictionary *item = self.animations[self.counter];
self.myLabel.alpha = 0;
self.myLabel.text = item[#"Text"];
[UIView animateWithDuration:0.3
animations:^{
self.myLabel.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3
delay:[item[#"Duration"] floatValue]
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.myLabel.alpha = 0.0;
} completion:^(BOOL finished) {
[self animate:++self.counter];
}];
}];
}
Try this code....
- (void)viewDidLoad {
[super viewDidLoad];
[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];
}];
}];
}];
}];
}];
}];
}];
}];
}

Dismiss a view with animation

I am popping the couponDetailsView with bounce animation. But i want to dismiss the view from left to right animation. How can i do this? Below is my source code. Any kind of help would be really helpful.
#pragma mark Bounce Animation
-(void) openContentDetailsView
{
[self.view bringSubviewToFront:self.couponDetailsView];
[UIView animateWithDuration:0.2 animations:
^(void){
self.couponDetailsView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.1f, 1.1f);
self.couponDetailsView.alpha = 0.5;
}
completion:^(BOOL finished){
[self bounceOutAnimationStoped];
}];
}
- (void)bounceOutAnimationStoped
{
[UIView animateWithDuration:0.1 animations:
^(void){
self.couponDetailsView.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.9, 0.9);
self.couponDetailsView.alpha = 0.8;
}
completion:^(BOOL finished){
[self bounceInAnimationStoped];
}];
}
- (void)bounceInAnimationStoped
{
[UIView animateWithDuration:0.1 animations:
^(void){
self.couponDetailsView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1, 1);
self.couponDetailsView.alpha = 1.0;
}
completion:^(BOOL finished){
[self animationStoped];
}];
}
- (void)animationStoped
{
}
- (IBAction)contentDetailsCloseButtonAction:(id)sender {
self.couponDetailsView.alpha = 0;
self.couponDetailsView.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.6, 0.6);
}
Try this:
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{
[testView setFrame:CGRectMake(x, y, width, height)];
} completion:^(BOOL finished) {
[testView removeFromSuperview];
}];

Recursive animateWithDuration: animations: completion: method iOS

I am attempting to make my item "glow" every 3 seconds changing its alpha from a high to low value and back. It however "flickers" and basically changes every 0.2 seconds from color to color randomly. I have pasted the method below. Its probably something to do with when completion is called but not sure?
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
//Disappear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.8;
self.loadingLabel.alpha = 0.3;
}
completion:^(BOOL finished){
//Appear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.3;
self.loadingLabel.alpha = 0.8;
}completion:^(BOOL finished2){
if(true){
[self showLoading];
}
}];
}]; }
self.loadingLabel.alpha = .8;
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
self.loadingLabel.alpha = .3;
} completion:nil];
UIViewAnimationOptionAutoreverse & UIViewAnimationOptionRepeat can help you.
you can stop it by
self.loadingLabel.alpha = .8;
or the other animation.
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.loadingLabel.alpha = .8;
} completion:nil];
UIViewAnimationOptionBeginFromCurrentState is a useful option for you to stop your animation by the other animation.
-(void)viewDidLoad{
//use schedule timer to call repeatedly showLoading method until loading done
}
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.3;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.8;
[UIView commitAnimations];
}

Transition view on the same ViewContoller

I am trying to flip transition on the same ViewController, I have a button for trigger transition. I want to change theme color when i press to button via flip transition.
i used this code part:
- (IBAction)changeThemeButtonTapped:(id)sender
{
int tempThemeColor = [[NSUserDefaults standardUserDefaults]integerForKey:#"themeColor"];
if (tempThemeColor == 10) {
[self loadTheme];
[UIView transitionFromView:self.view
toView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
NSLog(#"finished");
}];
}else{
[self loadTheme];
[UIView transitionFromView:self.view
toView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
completion:^(BOOL finished) {
NSLog(#"finished");
}];
}
}
when i press to button, view turns to black. Is there any way to do? What is my mistake?
Thank you for your answer and interest.
I found my question's answer.
if (tempThemeColor == 10) {
[self loadTheme];
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
} completion:^(BOOL finished) {
}];
}else{
[self loadTheme];
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
} completion:^(BOOL finished) {
}];
}
Before doing animation try to change the window/super view of the view's color
if (self.view.superview) {
self.view.superview.backgroundColor = [UIColor whiteColor];
}
self.view.window.backgroundColor = [UIColor whiteColor];

Animate UILabel with multiple sentences

I want to animate uilabel to change text every 1 second.
But when i run the app, it directly shows the last word. I.e. i have 4 different sentences to display one after another but it directly shows the last one.
This is the code that I am using
[UIView animateWithDuration:1.0
animations:^{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Retriving Coordinates";;
self.mapStat.alpha = 1.0f;
}];
[UIView animateWithDuration:1.0
animations:^{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Retrieved Coordinates";
self.mapStat.alpha = 1.0f;
}];
[UIView animateWithDuration:1.0
animations:^{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Applying Coordinates";
self.mapStat.alpha = 1.0f;
}];
[UIView animateWithDuration:1.0
animations:^{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Loading Map!";
self.mapStat.alpha = 1.0f;
}];
What is wrong with the code?
And is there any easier way to perform this animation?
Nothing is wrong with your code. What's wrong is that you, instead of reading the documentation, assumed that the text property of UILabel can be animated. It can't.
If you want to fade text in and out, you can use the animateWithDuration:animations:completion: method of UIView and change the text in the completion block, after the label has faded out.
The Problem is you not set the delay for next animation in your code,you set delay time for complete previous animations,like this
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Retriving Coordinates";
self.mapStat.alpha = 1.0f;
}
completion:^(BOOL finished)
{
}];
[UIView animateWithDuration:1.0f
delay:1.0f
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Retriving Coordinates";
self.mapStat.alpha = 1.0f;
}
completion:^(BOOL finished)
{
}];
[UIView animateWithDuration:1.0f
delay:2.0f
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Applying Coordinates";
self.mapStat.alpha = 1.0f;
}
completion:^(BOOL finished)
{
}];
[UIView animateWithDuration:1.0f
delay:2.0f
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.mapStat.alpha = 0.0f;
self.mapStat.text = #"Loading Map!";
self.mapStat.alpha = 1.0f;
}
completion:^(BOOL finished)
{
}];
Try this
int NoOfStringsLeft = 4;
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:Yes];
-(void)targetMethod:(NSTimer *)timer
{
//Update label text
NoOfStringsLeft --;
if(NoOfStringsLeft == 0)
{
[timer invalidate];
}
}

Resources