繁体   English   中英

在uitableviewcell中使用CABasicAnimation滚动UITableView可使动画跳动

[英]Scroll UITableView with CABasicAnimation in uitableviewcell makes the animation jumpy

我有一个UITableView,它的UITableViewCells内部是用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

任何需要的帮助将不胜感激。

由于我的声誉,我无法发表评论。 这不是主要问题的答案,而是代码中一个小错误的解决方案:如果您使用[NSTimer scheduledTimerWithTimeInterval:...] [NSTimer timerWithTimeInterval:...]而不是[NSTimer timerWithTimeInterval:...] ,则无需添加[NSRunLoop currentRunLoop]的计时器。 请参见《 NSTimer类参考 》中的“ 在运行循环中安排计时器 ”。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM