2014-10-22 82 views
0

我有一個UITableView,它的UITableViewCells中我用CABasicAnimation爲圖表餅圖層設置了動畫。在UITableViewCell中使用CABasicAnimation滾動UITableView使動畫變得跳躍

問題是,如果我在滾動表格視圖的同時對圖層進行動畫處理 - 圖層動畫變得跳躍而不是「乾淨」。

層的動畫開始在:

tableView:willDisplayCell:forRowAtIndexPath: 

使用dequeueReusableCellWithIdentifier還有:

static NSString *cellIdentifier = @"CellIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if(!cell) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    ... 
} 
... 
return cell; 

日程定時更新圖表的進展:

- (void)startAnimation 
{ 
self.elapsedTimeTimer = nil; 
self.elapsedTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 
                  target:self 
                  selector:@selector(updateProgress) 
                  userInfo:nil 
                  repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:self.elapsedTimeTimer 
          forMode:NSRunLoopCommonModes]; 
} 

- (void)updateProgress 
{ 
    if(!self.didEndProgress) 
     self.progress += 0.01; 

    if(self.progress >= self.finalPercents/100) 
    { 
     [self.elapsedTimeTimer invalidate]; 
     self.elapsedTimeTimer = nil; 
    } 
} 

層的實現:

@implementation CircleGraphLayer 
@dynamic progress; 

+ (BOOL)needsDisplayForKey:(NSString *)key { 
return [key isEqualToString:@"progress"] || [super needsDisplayForKey:key]; 
} 

- (id<CAAction>)actionForKey:(NSString *)aKey { 
if ([aKey isEqualToString:@"progress"]) { 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:aKey]; 
    animation.fromValue = [self.presentationLayer valueForKey:aKey]; 
    return animation; 
} 
return [super actionForKey:aKey]; 
} 

- (void)drawInContext:(CGContextRef)context { 

CGRect circleRect = CGRectInset(self.bounds, 1, 1); 
CGContextClearRect(context, circleRect); 

CGColorRef borderColor = [[UIColor colorWithWhite:1.0 alpha:0.4] CGColor]; 
CGColorRef backgroundColor = [[UIColor clearColor] CGColor]; //[[UIColor colorWithWhite: 1.0 alpha: 0.15] CGColor]; 

CGContextSetFillColorWithColor(context, backgroundColor); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetLineWidth(context, 1.0f); 

CGContextFillEllipseInRect(context, circleRect); 
CGContextStrokeEllipseInRect(context, circleRect); 

CGFloat radius = MIN(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect)); 
CGPoint center = CGPointMake(radius, CGRectGetMidY(circleRect)); 
CGFloat startAngle = -M_PI/2; 
CGFloat endAngle = self.progress * 2 * M_PI + startAngle; 
CGContextSetFillColorWithColor(context, borderColor); 
CGContextMoveToPoint(context, center.x, center.y); 
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0); 
CGContextClosePath(context); 
CGContextFillPath(context); 
[super drawInContext:context]; 
} 
@end 

任何必要的幫助將不勝感激。

+0

儀器告訴你什麼? – 2014-10-22 10:47:25

+0

我應該選擇哪種模板在樂器中運行?核心動畫? – PizzaByte 2014-10-22 12:11:14

回答

0

因爲我的聲望,我不能發表評論。這不是主要問題的答案,而是解決代碼中的一個小錯誤的方法:如果使用[NSTimer scheduledTimerWithTimeInterval:...]而不是[NSTimer timerWithTimeInterval:...],則不需要將計時器添加到[NSRunLoop currentRunLoop]。請參見中'運行循環中的計劃定時器'部分。 希望它有幫助。

+0

實際上,您可以看到OP正在將計時器添加到「NSRunLoopCommonModes」中。當你使用'scheduledTimer ...'時,它會被添加到'NSDefaultRunLoopMode'中,所以兩者不會相同。 – 2014-10-22 14:44:43