我是可可新手。我以为可以将UITableViewCell背景视图设置为我自己的自定义子类,以便在单元格的背景中显示加载栏,但是当我想更新栏时调用setNeedsDisplay似乎不足以满足我的需要。现在,我每0.1秒钟更新一次(这意味着我要在UIView中更新数据,然后调用setNeedsDisplay),并且每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);
}

最佳答案

大多数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调用中。

关于iphone - setNeedsDisplay:重绘周期之间过长,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15696690/

10-12 01:20