I am trying to create an animated barchart by scaling the bar (slider) and translating the endcap. The endcap works as expected, however, the "slider" translates when I try to scale it horizontally.
-(void) animateBarOpen: (NSInteger) rowIndex {
NSLog(@"%s: row=%d %@", __FUNCTION__, rowIndex, [self.values objectAtIndex:rowIndex]);
NSMutableDictionary *row = [self.values objectAtIndex:rowIndex];
UIImageView *slider = [row objectForKey:@"slider"];
UIImageView *endCap = [row objectForKey:@"image"];
UILabel *labelInfo = [row objectForKey:@"label"];
[labelInfo setText: [NSString stringWithFormat: @"%@ %@", [row objectForKey:@"quantity"], [row objectForKey:@"color"]]];
[labelInfo setTextColor: productColor];
float quantity = [(NSNumber*) [row objectForKey:@"quantity"] floatValue];
float tx = 10.0f * quantity;
float sx = -40.0f * quantity;
[UIView beginAnimations:@"open" context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
slider.transform = CGAffineTransformScale(CGAffineTransformIdentity, sx, 1.0f);
endCap.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, tx, 0.0f);
labelInfo.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, tx, 0.0f);
[UIView commitAnimations];
}
OK - I found a good ref for explaining the issue that allowed me to fix my code. Basically, since scaling is based upon the view's center property, it therefore translates the origin. As a result, you have to (re-)translate the view to the location you expected to translate as a ratio of the translation distance of location that the scaling moved.
Demystifying CGAffineTransform
Below is my code (that works) and you will notice a computation of the ratio of width before-to-after and then translating by that amount. Look at the computation for
sx
and the second computation oftx
(after the animation). I thought both should go in the animation, but that does not work as you might expect if the second translation is inside the begin/commit.Also note: I left some of the cosmetic code for displaying a colorful caption at the tip of the bar.
-