gpt4 book ai didi

iphone - 设置需求显示 : Too long between redraw cycles

转载 作者:行者123 更新时间:2023-11-29 04:02:24 36 4
gpt4 key购买 nike

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

这是我的绘制矩形代码(上面有一个辅助方法)和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 - 设置需求显示 : Too long between redraw cycles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696690/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com