2013-06-19 2 views
-1

Я рисую гистограмму, она отлично работает, за исключением того, что dosent имеет анимацию. Пример: fill colore 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) без upvote. – Caleb

+0

Извините, мой плохой, я наклеил неправильный код. Обновлен мой код. – iPhoneDev

ответ

5

Я считаю, что вы хотите анимировать высоты баров на барграфе. Я предлагаю вам реализовать каждый бар как отдельный UIview, простой прямоугольник. Вы также можете поместить все их в один вид с помощью пользовательского drawRect. Тогда вам необходимо масштабировать эти взгляды или изменить их рамку внутри анимации блока одним из следующих способов:

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

ИЛИ

+ (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!"); 
       }]; 
Смежные вопросы