0

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);
}

}

4

1 回答 1

2

不要简单地+animateWithDuration:animations:使用方便的方法,而是使用+animateWithDuration:delay:options:animations:completion:允许您传递UIViewAnimationOptionAllowAnimatedContent选项的更详细的方法。然后在块中更改视图的高度,animations以让视图和栏的高度在动画期间增长。

此选项专门用于告诉 CoreAnimation 在动画期间重绘您的内容(在动画drawRect:的每一步调用)。没有它,动画是在快照图像上完成的。


UIView 类参考文档:

UIViewAnimationOptionAllowAnimatedContent

通过动态更改属性值并重绘视图来为视图设置动画。如果此键不存在,则使用快照图像对视图进行动画处理。


UIView或者,您可以为每个条使用一个,并使用+animateWithDuration:animations:动画这些条框架,而不是使用 CoreGraphics 绘制它们(因为UIView动画方法用于动画视图(、、...)及其属性的属性framealphasubviews不是主要用于动画完成的绘图CoreGraphics

于 2013-08-11T17:05:45.803 回答