Reputation: 705
I have a view with an image shows some text in it. I need to perform animation which reveals the character by character in the end the complete text has to be displayed.
I have a logo view in my interface, over that I have animate view. for animate view the Leading, trailing, top and bottom constraints are set based on the superView (logo view).
I have tried following code but the duration which I have specified is not working properly.
- (void) animateLogo {
[UIView animateWithDuration:10.0 animations:^{
NSLog(@"animation started");
if (self.animateViewLeadingConstraint.constant < 94) { //94 is the total width of the logo view
self.animateViewLeadingConstraint.constant = self.animateViewLeadingConstraint.constant + 1;
} else {
self.animateViewLeadingConstraint.constant = 0;
}
} completion:^(BOOL finished) {
NSLog(@"animation ended");
[self.animateView layoutIfNeeded];
[self animateLogo];
}];
}
I have attached the log for time duration
2016-08-01 17:55:27.317 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.318 sample[20361:481531] animation ended
2016-08-01 17:55:27.318 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended
2016-08-01 17:55:27.319 sample[20361:481531] animation started
2016-08-01 17:55:27.319 sample[20361:481531] animation ended
I am not sure what mistake I have made in above code or am I misunderstood the UIView Animation concept
Any help or hint would greatly appreciated.
Upvotes: 0
Views: 658
Reputation: 705
Finally, I have got the solution based on @stevekohls comments
- (void) animateLogo {
self.animateViewLeadingConstraint.constant = 0;
[UIView animateWithDuration:5.0f animations:^{
self.animateViewLeadingConstraint.constant = 94.0;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f animations:^{
self.animateView.alpha = 0;
} completion:^(BOOL finished) {
self.animateViewLeadingConstraint.constant = 0;
[self.view layoutIfNeeded];
self.animateView.alpha = 1.0f;
[self animateLogo];
}];
}];
}
Upvotes: 0
Reputation: 2255
See this answer: How do I animate constraint changes?
You should be calling layoutIfNeeded
on the animated view's superview before your animation block, and in the animations:
block itself. The first call makes sure any layout changes are applied. Assuming logoView
is the superview of animateView
:
- (void) animateLogo {
self.animateViewLeadingConstraint.constant = 0.0;
[self.logoView layoutIfNeeded];
[UIView animateWithDuration:10.0 animations:^{
NSLog(@"animation started");
self.animateViewLeadingConstraint.constant = 94.0;
[self.logoView layoutIfNeeded];
} completion:^(BOOL finished) {
NSLog(@"animation ended");
}];
}
Upvotes: 1