我是可可新手。我想我可以将UITableViewCell背景视图设置为我自己的自定义子类,以便在单元格的背景中显示加载条,但是当我想要更新条形图时调用setNeedsDisplay
似乎不够快我的需要。现在我正在更新它(我的意思是更新UIView中的数据,然后每隔.1秒调用setNeedsDisplay
)并且每隔30-40秒才看到它实际更新。这是正常的吗?有没有办法加快速度?如果不是我还有其他选择吗?
这是我的绘制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);
}
答案 0 :(得分: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
。