gpt4 book ai didi

ios - setNeedsDisplay 不调用 drawRect

转载 作者:行者123 更新时间:2023-12-01 17:41:51 24 4
gpt4 key购买 nike

我正在尝试创建一个循环进度指示器。不幸的是,仅从 setNeedsDisplay 第一次和最后一次调用 drawRect 例程,它不会创建我正在寻找的渐变填充模式。我创建了一个 UIView 子类,在其中填充背景,然后更新绘制进度,如下所示:

- (void)drawRect:(CGRect)rect
{
// draw background
CGFloat lineWidth = 5.f;
UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
processBackgroundPath.lineWidth = lineWidth;
processBackgroundPath.lineCapStyle = kCGLineCapRound;
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.width / 2);
CGFloat radius = (self.bounds.size.width - lineWidth) / 2;
CGFloat startAngle = (2 * (float)M_PI / 2); // 90 degrees
CGFloat endAngle = (2 * (float)M_PI) + startAngle;
[processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[[UIColor grayColor] set];
[processBackgroundPath stroke];

// draw progress
UIBezierPath *processPath = [UIBezierPath bezierPath];
processPath.lineCapStyle = kCGLineCapRound;
processPath.lineWidth = lineWidth;
endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
[processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[[UIColor blackColor] set];
[processPath stroke];
}

我使用以下方法设置进度变量:
- (void)setProgress:(float)progress {
_progress = progress;
[self setNeedsDisplay];
}

然后,在将具有上述类的 UIView 分配给我的 Storyboard 后,我只需在主视图 Controller 中调用附加的方法:
- (void)progressView:(CircularProgress *)activityView loopTime:(CGFloat)duration repeats:(BOOL)repeat {
float portion = 0.0f;
while (portion < 1.0f) {
portion += 1/ (20.0 * duration);
[activityView setProgress:portion];
usleep(50000);
}
}

同样, [self setNeedsDisplay] 仅在第一次和最后一次调用 drawRect 。在此先感谢您的帮助。

最佳答案

usleep(50000)阻塞线程

使用NSTimer而是更新progressView。

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateProgressView) userInfo:nil repeats:YES];
duration = 0;
...


- (void)updateProgressView {
// Update the progress
}
}
...

关于ios - setNeedsDisplay 不调用 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716850/

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