How to animate constraints one by one in IOS? - ios

I have 6 views. I want to animate the constraints one by one, ie, i need the animation on second view only after first, then after second the third and so on. I have added the code in the completion handler of the animations, but it is not working. initial value of all the constraint is set to 300 in storyboard. I want to change it to 0 one by one. Now, only the first animation is working. This is what i have done.
layoutTopFirst.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopSecond.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopThird.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopFourth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopFifth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopSixth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}];
}];
}];
}];
}];
How to do animations one after another?

If only the first one is animating, make sure that
your other five constraint references all point to different vertical constraints ... if they were nil for example, you won't see any changes;
that they're all isActive;
that there aren't any other constraints that are preventing the updated constants from yielding changes.
For what it's worth, you might consider eliminating your tower of completion handlers with keyframe animation, e.g.
CGFloat relativeDuration = 1.0 / 6.0;
[UIView animateKeyframesWithDuration:6 delay:0 options:kNilOptions animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:relativeDuration animations:^{
self.topConstraint1.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:1.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint2.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:2.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint3.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:3.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint4.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:4.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint5.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:5.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint6.constant = 0;
[self.view layoutIfNeeded];
}];
} completion:nil];
Or, even simpler:
NSArray <NSLayoutConstraint *> *constraints = #[self.topConstraint1, self.topConstraint2, self.topConstraint3, self.topConstraint4, self.topConstraint5, self.topConstraint6];
CGFloat relativeDuration = 1.0 / (CGFloat) constraints.count;
[UIView animateKeyframesWithDuration:totalDuration delay:0 options:kNilOptions animations:^{
for (NSInteger i = 0; i < constraints.count; i++) {
[UIView addKeyframeWithRelativeStartTime: ((CGFloat) i) * relativeDuration relativeDuration:relativeDuration animations:^{
constraints[i].constant = 0;
[self.view layoutIfNeeded];
}];
}
} completion:nil];
That yields:

It would help if you showed all your code (how you setup the constraints, etc).
But, here is a quick example.
It creates 6 labels, with top constraints at 100. Tap the "Do Anim" button to animated them to Top: 0.0 ... tap again to animate back to 100:
#import "SequentialAnimViewController.h"
#interface SequentialAnimViewController () {
NSMutableArray *views;
NSMutableArray *topConstraints;
}
#end
#implementation SequentialAnimViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self action:#selector(animViews) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Do Anim" forState:UIControlStateNormal];
button.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
button.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button];
[button.centerXAnchor constraintEqualToAnchor:[self.view centerXAnchor]].active = YES;
[button.centerYAnchor constraintEqualToAnchor:[self.view centerYAnchor]].active = YES;
[button.widthAnchor constraintEqualToConstant:200.0].active = YES;
views = [NSMutableArray new];
topConstraints = [NSMutableArray new];
CGFloat x = 40.0;
CGFloat w = 40.0;
UILayoutGuide *g = [self.view safeAreaLayoutGuide];
for (int i = 0; i < 6; i++) {
UILabel *v = [UILabel new];
v.backgroundColor = [UIColor blueColor];
v.textColor = [UIColor yellowColor];
v.textAlignment = NSTextAlignmentCenter;
v.text = [NSString stringWithFormat:#"%i", i];
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:v];
[v.widthAnchor constraintEqualToConstant:w].active = YES;
[v.heightAnchor constraintEqualToConstant:w].active = YES;
[v.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:x].active = YES;
[topConstraints addObject:[v.topAnchor constraintEqualToAnchor:g.topAnchor constant:100.0]];
[views addObject:v];
x += w + 8;
}
[NSLayoutConstraint activateConstraints:topConstraints];
}
-(void)animViews {
__block int i = 0;
__block CGFloat newConstant = ((NSLayoutConstraint *)self->topConstraints[0]).constant == 0.0 ? 100.0 : 0.0;
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
NSLog(#"All Done!");
}];
}];
}];
}];
}];
}];
}
#end

Related

Trouble executing all animation's using animateWithDuration

I'm having some issues with my animations. The intended result is that the UIButton's should both move to their positions, and once they reach their positions they must oscillate upwards and downwards, subtly, to simulate reaching the end of their bungee chord, if you will. The result I'm achieving is that every animation apart from the oscillation is executing perfectly. Why is this? (The oscillation animation I refer to is, of course, the spring animateWithDuration method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_orText.alpha = 0;
//Email login button properties
_emailLoginForwardButton.layer.cornerRadius = _emailLoginForwardButton.frame.size.width / 2;
self.emailLoginForwardButton.layer.borderWidth = 1.5f;
self.emailLoginForwardButton.layer.borderColor = [UIColor whiteColor].CGColor;
[self.emailLoginForwardButton setImageEdgeInsets:UIEdgeInsetsMake(7, 7, 7, 7)];
_emailLoginForwardButton.frame = CGRectMake(77, 700, 40, 40);
[UIView animateWithDuration:0.5
animations:^
{
_emailLoginForwardButton.frame = CGRectMake(77, 470, 40, 40);
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:0.3
delay:0.6
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^(void){}
completion:^(BOOL finished)
{
_orText.text = #"or";
[UIView animateWithDuration:0.4
animations:^
{
[_orText setAlpha:1];
}
];
}
];
}
];
//Google login button properties
_googleLoginForwardButton.layer.cornerRadius =_googleLoginForwardButton.frame.size.width / 2;
self.googleLoginForwardButton.layer.borderWidth = 1.5f;
self.googleLoginForwardButton.layer.borderColor = [UIColor grayColor].CGColor;
_googleLoginForwardButton.frame = CGRectMake(257, 700, 40, 40);
[UIView animateWithDuration:0.5
delay:0.4
options:0
animations:^
{
_googleLoginForwardButton.frame = CGRectMake(257, 470, 40, 40);
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:0.3
delay:0.3
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^
{
}
completion:^(BOOL finished)
{
}
];
}
];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.googleLoginForwardButtonBottomConstraints.constant = -100.0;//e.g
self.emailLoginForwardButtonBottomConstraints.constant = -100.0;//e.g
[UIView animateWithDuration:0.6
delay:0.6
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^(void){
self.googleLoginForwardButtonBottomConstraints.constant = 40.0;//e.g
self.emailLoginForwardButtonBottomConstraints.constant = 40.0;//e.g
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
}
I have run this code and its working. Hope it will help you.
[UIView animateWithDuration:1
delay:0.6
usingSpringWithDamping:0.42
initialSpringVelocity:0.1
options:0
animations:^(void)
{
_orText.text = #"or";
[UIView animateWithDuration:0.7
animations:^
{
[_orText setAlpha:1];
}];
[UIView animateWithDuration:0.6
animations:^
{
_emailLoginForwardButton.frame = CGRectMake(77, 470, 40, 40);
}];
[UIView animateWithDuration:0.6
animations:^
{
_googleLoginForwardButton.frame = CGRectMake(257, 470, 40, 40);
}
];
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
This was my solution, however there was more I wanted to do with this code. I wanted to make both buttons move at different intervals, and an if statement won't work here, for some reason. Also, I only wanted this to initialise when another action has taken place, which is still possible to do, but I'd prefer to be able to make them move at different times.

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

How to change the frame of a view with animations?

I am a beginner. I am updating the frame of a view. I am a bit confused about animations. I just want to know how to update the frame of a view with any kind of animations.
This is where I am updating my frames:
- (IBAction)btnCountries:(id)sender {
_dropDownView.hidden = NO;
fromCountry=YES;
fromCity = NO;
fromState = NO;
_dropDownView.frame = CGRectMake(132.0f, 63.0f, 172.0f, 308.0f);
[self.tblDropDown reloadData];
}
Any help is highly appreciated.
You can use UIView method animationWithDuration:
or CoreAnimation framework.
[UIView animateWithDuration:0.5 animations:^{
// animation here
_dropDownView.frame = CGRectMake(132.0f, 63.0f, 172.0f, 308.0f);
}];
[self.view setUserInteractionEnabled:NO];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
_dropDownView.frame = CGRectMake(132.0f, 63.0f, 172.0f, 308.0f);
}
completion:^(BOOL finished) {
[self.view setUserInteractionEnabled:YES];
}];
UIView support animation like as follows,
_dropDownView.hidden = NO;fromCountry=YES;
fromCity = NO;
fromState = NO;
UIView animateWithDuration:1.0f animations:^
{
_dropDownView.frame = CGRectMake(132.0f, 63.0f, 172.0f, 308.0f);
} completion:^(BOOL finished)
{
[self.tblDropDown reloadData];
}
Try below code
- (IBAction)btnCountries:(id)sender {
_dropDownView.hidden = NO;
fromCountry=YES;
fromCity = NO;
fromState = NO;
[UIView animateWithDuration:2.0
animations:^{
CGRect frame = _dropDownView.frame;
frame=CGRectMake(132.0f, 63.0f, 172.0f, 308.0f);
_dropDownView.frame = frame;
}
completion:^(BOOL finished){
[self.tblDropDown reloadData];
}];
}
Hope it helps you..

iOS Views return to original locations when textView becomesFirstResponder

Hello and thank you in advance...
I am moving views frame location in animation blocks. After I move views, I am tapping a textView to allow for editing/input. When the textView is tapped, the views I moved previously pop back to their original (unmoved) frame locations.
I'm not sure what I am doing incorrectly here.
THIS IS HOW I SETUP THE DIFFERENT FRAME POSITIONS TO ANIMATE TO
//Books active and rest position will effect other elements (paper and paperTextView)
self.bookRestPosition = CGRectMake((self.view.frame.size.width * 0.10), (self.view.frame.size.height * 0.4), self.view.frame.size.width, self.view.frame.size.height);
self.bookActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +100, self.view.frame.size.width, self.view.frame.size.height);
self.bookOpenPosition = CGRectMake(self.bookActivePosition.origin.x, self.bookActivePosition.origin.y, 20, self.bookActivePosition.size.height);
self.quillRestPosition = self.quill.frame;
self.quillActivePosition = CGRectMake(self.quill.frame.origin.x, self.view.frame.origin.y -200, self.quill.frame.size.width, self.quill.frame.size.height);
self.woodTableRestPosition = self.view.frame;
self.woodTableActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200, self.view.frame.size.width, self.view.frame.size.height +200);
self.paperRestPosition = self.bookRestPosition;
self.paperActivePosition = CGRectMake(self.bookOpenPosition.size.width, self.bookActivePosition.origin.y, self.bookActivePosition.size.width, self.bookActivePosition.size.height);
self.paperTextViewRestPosition = self.bookRestPosition;
self.paperTextViewActivePosition = self.paperActivePosition;
self.paperTextView.editable = NO;
self.paper.alpha = 0.0;
self.paperTextView.alpha = 0.0;
self.paper.frame = self.bookRestPosition;
self.paperTextView.frame = self.bookRestPosition;
THIS IS HOW I ANIMATE THEM
- (IBAction)onBookTapped:(UITapGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
else if (self.logCover.frame.origin.x == 0){
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)swipeBookLeft:(UISwipeGestureRecognizer *)sender {
//if (self.logCoverImage.frame.origin.x == 0) {
self.paper.alpha = 1.0;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookOpenPosition];
self.editButton.alpha = 1.0;
self.editButton.enabled = YES;
} completion:^(BOOL finished) {
NSLog(#"Book Has Opened!");
//This must be here. The paperTextView is what the swipe right gesture is attached to
self.paperTextView.alpha = 1.0;
// self.logCoverImage.alpha = 0.0;
// self.quill.alpha = 0.0;
}];
//}
}
- (IBAction)onPaperSwipeRight:(UISwipeGestureRecognizer *)sender {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookActivePosition];
self.editButton.alpha = 0.0;
self.editButton.enabled = NO;
} completion:^(BOOL finished) {
NSLog(#"Book Has Closed!");
//self.paper.alpha = 0.0;
// [NSUserDefaults *log = [NSUserDefaults standardUserDefaults];
// [log setObject:self.paperTextView forKey:#"textLog"];
}];
}
- (IBAction)onBookSwipeDown:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == 0) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)onBookSwipeUp:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
}
- (void)animateAtStartup
{
self.logCover.frame = CGRectMake(self.view.frame.size.width, self.view.frame.size.height, self.bookRestPosition.size.width, self.bookRestPosition.size.height);
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
AND THIS IS WHAT HAPPENS WHEN THE EDIT BUTTON IS PRESSED
- (IBAction)onEditButtonPressed:(id)sender
{
self.paperTextView.editable = YES;
[self.paperTextView becomeFirstResponder];
}

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