在UITableViewCell中使用CABasicAnimation滚动UITableView使动画变得跳跃

问题描述:

我有一个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

因为我的声望,我不能发表评论。这不是主要问题的答案,而是解决代码中的一个小错误的方法:如果使用[NSTimer scheduledTimerWithTimeInterval:...]而不是[NSTimer timerWithTimeInterval:...],则不需要将计时器添加到[NSRunLoop currentRunLoop]。请参见中'运行循环中的计划定时器'部分。 希望它有帮助。

+0

实际上,您可以看到OP正在将计时器添加到“NSRunLoopCommonModes”中。当你使用'scheduledTimer ...'时,它会被添加到'NSDefaultRunLoopMode'中,所以两者不会相同。 – 2014-10-22 14:44:43