2013-06-19 75 views
-1

我正在繪製條形圖,除了dosent有動畫外,它工作正常。例如:填充顏色0-50%。 我使用簡單的drawRect方法,在此提請是我的代碼:iOS中的動畫條形圖

- (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

那不是你的*代碼。您逐字從[我的答案](http://stackoverflow.com/a/17175206/643383)複製到[您的上一個問題](http://stackoverflow.com/q/17174468/643383),沒有那麼多給予好評。 – Caleb

+0

對不起,我粘貼了錯誤的代碼。現在更新我的代碼。 – iPhoneDev

回答

5

我相信你想要在條形圖中爲條形的高度設置動畫。 我建議你實現每個酒吧作爲一個單獨的UIview,一個簡單的矩形。您也可以將其全部放在一個具有自定義drawRect的視圖中。然後,你需要擴展這些意見或更改的下列方法的動畫塊內部的框架:

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

OR

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

對於一個偉大的教程,請參閱this

這是一個例子,如果你沒有很多時間。

[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!"); 
       }];