I have a scene of storyboard with a UIVIewcontroller. Inside this scene i have a UIImageview that contains the background image, an UIButton, and an UIView.
This UIView have a overrided drawRect method with:
- (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);
}
I've tried animate the grow up of bars but not work..my question is: how can i make this work?
Regards
----UPDATED WITH NEW CODE BUT NOT WORKING ALREADY
---CODE OF GRAPHVIEW
- (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++;
}
---CODE OF BAR VIEW
- (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);
}
}