-1

我正在绘制条形图,它工作正常,除了它没有动画。例如:填充颜色 0-50%。我正在使用简单的 DrawRect 方法来绘制这里是我的代码:

- (void)drawRect:(CGRect)rect
{
    CGFloat height = self.bounds.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [self colorWithR:149 G:21 B:29 A:1].CGColor);

CGFloat barWidth = 52;
int count = 0;
NSNumber *num = [NSNumber numberWithFloat:graphValue];

CGFloat x = count * (barWidth + 10);
CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
CGContextAddRect(context, barRect);
count++;

CGContextFillPath(context);

}

请帮助我知道添加动画的简单方法。

4

1 回答 1

6

我相信您想为条形图中的条形高度设置动画。我建议你将每个栏实现为一个单独的 UIview,一个简单的矩形。您还可以使用自定义 drawRect 将它们全部放在一个视图中。然后您需要在以下任一方法的动画块内缩放这些视图或更改它们的框架:

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

或者

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

对于一个伟大的教程看到这个

如果您没有很多时间,这是一个示例。

[UIView animateWithDuration:1//Amount of time the animation takes.
                      delay:0//Amount of time after which animation starts.
                    options: UIViewAnimationCurveEaseOut//How the animation will behave.
                 animations:^{
                    //here you can either set a CGAffineTransform, or change your view's frame.
                    //Both will work just fine.
                    yourBarView.transform = CGAffineTransformMakeScale (
                    scaleForX,//say 1, you dont want it to change.
                    scaleForY//say 20, you want to make it 20 times larger in the y                                               
                    //direction.
                    //Note* to animate from a zero height to a finite height change the                  
                    //view's frame.
                    //yourBarView.frame = CGRectMake(0,0,20,100);
                    );
                 }
                 completion:^(BOOL finished){//This block is called when the animation completes.
                     NSLog(@"Done!");
                 }];
于 2013-06-19T04:08:26.330 回答