【发布时间】:2014-04-14 07:34:30
【问题描述】:
我在父视图中定位两个子视图时遇到了麻烦。它们旨在包含一个基本的动画条形图,并且动画部分似乎运行良好。同样,相应的条形似乎准确地反映了数据。
但是,我试图将两个视图的原点基于父视图的下边缘(屏幕截图中的灰色区域),结果似乎是随机的。我希望它不是真正随机的,但我无法确定它。
代码如下:
-(void) makeBarChart
{
RiserBar *thisRiser;
for(int i=0; i<2; i++)
{
thisRiser = [[RiserBar alloc] initWithFrame:CGRectZero];
thisRiser.tag = i+1;
[self.chartView addSubview:thisRiser];
// [self placeAndAnimateThisBar];
}
int barWidth = self.chartView.bounds.size.width /((2 * 2) -1);
int focusBarEndHeight = (self.chartView.bounds.size.height-20) * (focusActivityPercent / 100);
int benchmarkBarEndHeight = (self.chartView.bounds.size.height-20) * (benchmarkActivityPercent / 100);
for (thisRiser in self.chartView.subviews)
{
switch (thisRiser.tag)
{
case 1:
{ [UIView animateWithDuration:1
delay:.2
options: UIViewAnimationCurveEaseOut
animations:^
{
thisRiser.frame = CGRectMake(20, self.chartView.frame.origin.y, barWidth, 0);
thisRiser.backgroundColor = [UIColor blackColor];
thisRiser.frame = CGRectMake(20, self.chartView.frame.origin.y, barWidth, -focusBarEndHeight);
thisRiser.backgroundColor = [UIColor redColor];
}
completion:^(BOOL finished)
{
NSLog(@"Done!");
}];
}
break;
case 2:
{
[UIView animateWithDuration:1
delay:.2
options: UIViewAnimationCurveEaseOut
animations:^
{
thisRiser.frame = CGRectMake(barWidth + 40, self.chartView.frame.origin.y, barWidth, 0);//load frame from data
thisRiser.backgroundColor = [UIColor blackColor];
thisRiser.frame = CGRectMake(barWidth + 40, self.chartView.frame.origin.y, barWidth, -benchmarkBarEndHeight);
thisRiser.backgroundColor = [UIColor blueColor];
}
completion:^(BOOL finished)
{
NSLog(@"Done!");
}];
}
break;
case 3:
//do something to the view
break;
//and so on...
default:
break;
}
}
}
以及生成的屏幕截图:
我应该提到,红色和蓝色条的意图是从灰色父视图的底部开始并上升。我并不完全清楚为什么我必须反转.heightproperty 的极性来实现这一点。鉴于此代码,为什么原点位于父视图中间附近?
谁能指出我的错误?
谢谢!
根据答案 2 中 @Rob 的温和指导进行编辑:
这是我代码相关部分的更改,也受到this slideshow 中信息的影响,我强烈推荐给像我一样喜欢图片而不是详细解释的任何人:
switch (thisRiser.tag)
{
case 1:
{ [UIView animateWithDuration:1
delay:.2
options: UIViewAnimationCurveEaseOut
animations:^
{
thisRiser.frame = CGRectMake(20, self.chartView.frame.size.height, barWidth, 0);
thisRiser.backgroundColor = [UIColor blackColor];
thisRiser.frame = CGRectMake(20, self.chartView.frame.size.height, barWidth, -focusBarEndHeight);
thisRiser.backgroundColor = [UIColor redColor];
}
completion:^(BOOL finished)
{
NSLog(@"Done!");
}];
}
break;
case 2:
{
[UIView animateWithDuration:1
delay:.2
options: UIViewAnimationCurveEaseOut
animations:^
{
thisRiser.frame = CGRectMake(barWidth + 40, self.chartView.frame.size.height, barWidth, 0);//load frame from data
thisRiser.backgroundColor = [UIColor blackColor];
thisRiser.frame = CGRectMake(barWidth + 40, self.chartView.frame.size.height, barWidth, -benchmarkBarEndHeight);
thisRiser.backgroundColor = [UIColor blueColor];
}
completion:^(BOOL finished)
{
NSLog(@"Done!");
}];
}
break;
case 3:
//do something to the view
break;
//and so on...
default:
break;
}
这就是它现在的样子(图中条形高度的差异是由于不同的数据):
再次感谢罗伯!
【问题讨论】: