【发布时间】:2013-08-11 14:42:16
【问题描述】:
我有一个带有 UIVIewcontroller 的情节提要场景。在这个场景中,我有一个 UIImageview,其中包含背景图像、一个 UIButton 和一个 UIView。
这个 UIView 有一个覆盖的 drawRect 方法:
- (void)drawRect:(CGRect)rect
{
CGFloat height = self.bounds.size.height-15; // - 15 for text space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
UIColor *barColor = [UIColor colorWithRed:226.0/255.0 green:178.0/255.0 blue:39.0/255.0 alpha:1.0];
CGContextSetFillColorWithColor(context, barColor.CGColor);
CGFloat barWidth = 37.5;
[UIView animateWithDuration:1.0 animations:^{
int count = 0;
NSArray *values = [NSArray arrayWithObjects:@1, @0.5, @0.2, @0.7, nil];
for (NSNumber *num in values) {
CGFloat x = count * (barWidth + 18);
CGFloat startY = (height - ([num floatValue] * height));
CGRect barRect = CGRectMake(x, startY+15, barWidth, [num floatValue] * height);
CGContextAddRect(context, barRect);
// save context state first
CGContextSaveGState(context);
[self drawWords:[NSString stringWithFormat:@"%@%%",num] AtPoint:CGPointMake(x + (barWidth/2)-10 , startY) color:[UIColor whiteColor]];
count++;
// restore context state first
CGContextRestoreGState(context);
}
}];
CGContextFillPath(context);
}
我尝试过为酒吧的成长设置动画,但没有成功。我的问题是:我怎样才能让这个工作?
问候
----使用新代码更新,但已经无法使用
---图形代码
- (void) initHelper {
height=0;
graphBars = [[NSMutableArray alloc] initWithCapacity:0];
[self performSelector:@selector(animateBars) withObject:NULL afterDelay:0.5];
}
- (void) animateBars
{
for (GraphBar *bar in graphBars)
{
bar.bottom+=bar.height;
}
CGFloat barWidth=37.5;
CGFloat rat = self.frame.size.height*0.95;
[UIView animateWithDuration:1.0 animations:^{
NSUInteger _index = 0;
for (GraphBar *bar in graphBars)
{
bar.frame = CGRectMake(_index*(barWidth+18), self.frame.size.height-roundf(bar.barValue*rat), barWidth, roundf(bar.barValue*rat));
_index++;
}
}];
}
- (void) layoutSubviews
{
[super layoutSubviews];
CGFloat barWidth=37.5;
CGFloat rat = self.frame.size.height*0.01;
NSUInteger _index = 0;
for (GraphBar *bar in graphBars)
{
bar.frame = CGRectMake(_index*(barWidth+18), self.frame.size.height-roundf(bar.barValue*rat), barWidth, roundf(bar.barValue*rat));
_index++;
}
---条形图代码
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
UIColor *barColor = [UIColor colorWithRed:226.0/255.0 green:178.0/255.0 blue:39.0/255.0 alpha:1.0];
CGContextSetFillColorWithColor(context, barColor.CGColor);
CGContextAddRect(context, rect);
CGContextFillPath(context);
}
}
【问题讨论】: