0

我是可可的新手。我想我可以将 UITableViewCell 背景视图设置为我自己的自定义子类,以便在单元格的背景中显示一个加载栏,但是当我想要更新栏时调用似乎setNeedsDisplay不足以满足我的需要。现在我setNeedsDisplay每 0.1 秒更新一次(我的意思是更新 UIView 中的数据,然后调用),并且每 30-40 秒才看到它实际更新一次。这是正常的吗?有什么办法可以加快速度吗?如果没有,我想做的还有其他选择吗?

这是我的 draw rect 代码(上面有一个辅助方法)和updateBarAt,它被称为更新它:

-(void) updateBarAt: (float) playHead {
    self.playHead = playHead;
    NSLog(@"%f", self.playHead);
    [self setNeedsDisplay];
}

-(void) drawBackground: (CGRect) rect{
    double totalTime = self.playFor + self.wait;

    double waitPercentage = self.wait / totalTime;
    double playForPercentage = (self.playFor - self.fadeIn - self.fadeOut) / totalTime;
    double fadeInPercentage = self.fadeIn / totalTime;
    double fadeOutPercentage = self.fadeOut / totalTime;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect blueRect = CGRectMake((waitPercentage+fadeInPercentage)*rect.size.width, 0, playForPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, .639, .737, 1, 1.0);
    CGContextFillRect(context, blueRect);
    CGRect greenRect = CGRectMake(0, 0, waitPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, .843, 1, .651, 1.0);
    CGContextFillRect(context, greenRect);


    CGRect purpleRect = CGRectMake(waitPercentage*rect.size.width+.1, 0, fadeInPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, .7451, .639, 1.0, 1.0);
    CGContextFillRect(context, purpleRect);

    CGRect purpleR = CGRectMake((1.0 - fadeOutPercentage)*rect.size.width - .5, 0, fadeOutPercentage*rect.size.width + .5, rect.size.height);
    CGContextFillRect(context, purpleR);
}

- (void)drawRect:(CGRect)rect { //just does blue and green, need to add purple. Research
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, rect);


    if (self.playHead == -1)
        return;
    [super drawRect:rect];

    double totalTime = self.playFor + self.wait;
    double playedPercentage = self.playHead / totalTime;

    double waitPercentage = self.wait / totalTime;
    double playForPercentage = (self.playFor - self.fadeIn - self.fadeOut) / totalTime;
    double fadeInPercentage = self.fadeIn / totalTime;
    double fadeOutPercentage = self.fadeOut / totalTime;

    NSLog(@"Played: %f Wait: %f Play: %f Fade In: %f Fade Out: %f", playedPercentage, waitPercentage, playForPercentage, fadeInPercentage, fadeOutPercentage);

    if (playedPercentage <= waitPercentage) {
        waitPercentage = playedPercentage;
        playForPercentage = 0;
        fadeInPercentage = 0;
        fadeOutPercentage = 0;
    } else if (playedPercentage <= waitPercentage+fadeInPercentage) {
        playForPercentage = 0;
        fadeInPercentage = playedPercentage - waitPercentage;
        fadeOutPercentage = 0;
    } else if (playedPercentage <= waitPercentage+fadeInPercentage + playForPercentage) {
        playForPercentage = playedPercentage - waitPercentage - fadeInPercentage;
        fadeOutPercentage = 0;
    } else {
        fadeOutPercentage = playedPercentage - waitPercentage - playForPercentage;
    }

    //[self drawBackground:rect];

    CGRect blueRect = CGRectMake((waitPercentage+fadeInPercentage)*rect.size.width, 0, playForPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextFillRect(context, blueRect);

    CGRect greenRect = CGRectMake(0, 0, waitPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
    CGContextFillRect(context, greenRect);


    CGRect purpleRect = CGRectMake(waitPercentage*rect.size.width+.1, 0, fadeInPercentage*rect.size.width, rect.size.height);
    CGContextSetRGBFillColor(context, 0.33, 0.1, .55, 1.0);
    CGContextFillRect(context, purpleRect);

    CGRect purpleR = CGRectMake((1.0 - fadeOutPercentage)*rect.size.width - .5, 0, fadeOutPercentage*rect.size.width + .5, rect.size.height);
    CGContextFillRect(context, purpleR);
} 
4

1 回答 1

1

大多数UIKit使用需要在主线程上完成。很可能您的updateBarAt:方法没有在主线程上被调用,这意味着您的调用setNeedsDisplay是在某个后台线程上完成的。

一种可能的解决方案是:

- (void)updateBarAt:(float)playHead {
    self.playHead = playHead;
    NSLog(@"%f", self.playHead);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setNeedsDisplay];
    });
}

另一种选择是将调用包装到调用updateBarAt:dispatch_async

于 2013-03-29T03:52:38.207 回答