好的 - 我找到了一个很好的参考来解释允许我修复代码的问题。基本上,由于缩放是基于视图的中心属性,因此它会平移原点。因此,您必须将视图(重新)平移到您希望平移的位置,作为缩放移动位置的平移距离的比率。
揭秘 CGAffineTransform
下面是我的代码(有效),您会注意到前后宽度比的计算,然后按该数量进行转换。查看(动画之后)的计算sx
和第二次计算。tx
我认为两者都应该出现在动画中,但是如果第二个翻译在开始/提交中,那将无法正常工作。
另请注意:我留下了一些装饰代码,用于在栏的顶端显示彩色标题。
-
(void) animateBarOpen: (NSInteger) rowIndex withDuration: (NSTimeInterval) duration {
if (VERBOSE_LOG) {
NSLog(@"%s: row=%d %@", __FUNCTION__, rowIndex, [self.values objectAtIndex:rowIndex]);
}
NSMutableDictionary *rowValues = [self.values objectAtIndex:rowIndex];
NSMutableDictionary *rowOutlets = [self.iboutlets objectAtIndex:rowIndex];
UIColor *productColor = [InfographView colorByName: [rowValues objectForKey: @"color"]];
UIImageView *slider = [rowOutlets objectForKey:@"slider"];
UIImageView *endCap = [rowOutlets objectForKey:@"image"];
UILabel *labelInfo = [rowOutlets objectForKey:@"label"];
NSAssert1(labelInfo != nil, @"labelInfo is NULL??", nil);
[labelInfo setBackgroundColor: productColor];
// (255, 253, 208)
UIColor *creamColor = [UIColor colorWithRed:255.0/255.0f green:253.0/255.0f blue:208.0/255.0f alpha:0.9];
[labelInfo setTextColor: creamColor];
if ([(NSString*)[rowValues objectForKey:@"color"] isEqualToString:@"Black"]) {
[labelInfo setShadowColor: [UIColor grayColor]];
} else {
[labelInfo setShadowColor: [UIColor blackColor]];
}
[labelInfo setText: [NSString stringWithFormat: @"%@ %@", [rowValues objectForKey:@"quantity"], [rowValues objectForKey:@"color"]]];
[slider setHidden:YES];
[slider setAlpha: 0.7f];
float rangeScale = [self computeRangeScale: self.values forWidth: CGRectGetWidth(self.frame) - MARGINS];
slider.transform = CGAffineTransformIdentity;
float quantity = [(NSNumber*) [rowValues objectForKey:@"quantity"] floatValue];
float tx = rangeScale * quantity;
float sx = tx / CGRectGetWidth(slider.frame); // (CGRectGetWidth(endCap.frame) - CGRectGetWidth(slider.frame));
float sliderWidth = CGRectGetWidth(slider.frame);
[slider setHidden:(rowIndex == -1)];
[UIView beginAnimations:@"open" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
slider.transform = CGAffineTransformScale(CGAffineTransformIdentity, sx, 1.0f);
endCap.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, tx, 0.0f);
labelInfo.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, tx, (-rowIndex*1.0f));
[labelInfo setAlpha:1.0];
[UIView commitAnimations];
tx = 1.0f * (CGRectGetWidth(slider.frame) - sliderWidth) / (sx*2.0f); slider.transform = CGAffineTransformTranslate(slider.transform, tx, 0.0f);
[slider setHidden:NO];
}