I have a vertical rectangle -- a simple UIView -- that is divided into 4 sections, sort of like a pie chart, and each section will grow and shrink dynamically (as data rolls in), and I'm trying to get that to happen smoothly. Am using constraints to keep their sides united tightly to one another.
Part of the animation happens smoothly, but initially the four colored sections, which are just empty UILabel objects, are resized abruptly, revealing the background color of the container and then the animation seems to kick in and resolve the boundaries of the UILabel objects smoothly. I have a good video captured from the Simulator that shows the behavior, but don't have a way to provide that in the question here. Link perhaps coming later. The animation code right now is very simple. When a timer fires I simply alternate between two different states wherein I assign the constant value for the height constraints. Like so:
-(void)relayoutSubviewsAnimated {
static int ctr = 1;
[self layoutIfNeeded];
[UIView animateWithDuration:1.5
animations:^{
if (ctr == 1) {
self->_outletBucketMastersHeight.constant = 0.25 * nHeightOfPieBar;
self->_outletBucketMeetsHeight.constant = 0.25 * nHeightOfPieBar;
self->_outletBucketApproachesHeight.constant = 0.25 * nHeightOfPieBar;
self->_outletBucketDidNotMeetHeight.constant = 0.25 * nHeightOfPieBar;
ctr++;
}
else if (ctr == 2) {
self->_outletBucketMastersHeight.constant = 0.2 * nHeightOfPieBar;
self->_outletBucketMeetsHeight.constant = 0.1 * nHeightOfPieBar;
self->_outletBucketApproachesHeight.constant = 0.4 * nHeightOfPieBar;
self->_outletBucketDidNotMeetHeight.constant = 0.4 * nHeightOfPieBar;
ctr = 1;
}
[self layoutIfNeeded];
}];
}
So, initially the sections will resize suddenly, with no animation, and will momentarily look like so:
but will then smoothly animate the sizes until everything looks correct, like so:
The other usual constraints (horizontal and vertical space constraints) bind the UILabel objects to each other and leading and trailing constraints bind the UILabel objects to the sides of their container.
What could I be doing wrong? How do I smoothly animate the growth and shrinkage of these 4 UILabels without the white background of the container suddenly showing through? I have read a number of SO questions and other articles.


As I mentioned in my comments, this is what I would call a BUG.
When we animate the height of a
UILabel:Quick demonstration:
It looks like this when running:
Tapping anywhere will toggle the Height constraint constants between 300 and 100 and animate to the new values.
UIView... it animates as expectedUILabel... you'll see it snapUIViewwith aUILabelas a subview. It gives us the desired animations.Here's an example to achieve your layout, using a simple
UIViewsubclass to hold the "centered" labels:and an example controller:
That looks like this:
Each tap will cycle to the next set of percentages.
Edit - because I hate answering an Obj-C question with Swift code...
Here is a similar implementation as above, with a few "enhancements."
[1, 1, 1, 1]each bar height will be25%[5, 10, 15, 20]the bar heights will be10%, 20% 30%, 40%EmbeddedLabelView.h
EmbeddedLabelView.m
LabelBarsView.h
LabelBarsView.m
LabelBarsViewController.h
#import <UIKit/UIKit.h>
LabelBarsViewController.m
Looks like this when running - each tap cycles to the next values set: