在uitableviewcell中使用CABasicAnimation滚动UITableView会使动画变得浑浊

时间:2014-10-22 10:05:32

标签: ios objective-c uitableview cabasicanimation

我在UITableViewCells中有一个UITableView,我用CABasicAnimation设置图表饼图层。

问题是如果我在动画图层时滚动表格视图 - 图层动画变得跳跃而不是“干净”。

图层的动画从:

开始
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

任何需要的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

由于我的声誉,我无法发表评论。这不是主要问题的答案,而是代码中的小错误的解决方案:如果您使用[NSTimer scheduledTimerWithTimeInterval:...]代替[NSTimer timerWithTimeInterval:...],则不需要将计时器添加到[NSRunLoop currentRunLoop]。请参阅&#39; 在运行循环中安排计时器&#39; NSTimer Class Reference中的部分。 希望它有所帮助。