setNeedsDisplay不调用drawRect

Jac*_*120 1 objective-c ios

我正在尝试创建循环进度指示器.不幸的是,drawRect例程仅从setNeedsDisplay调用第一次和最后一次,它不会创建我正在寻找的渐进填充模式.我已经创建了一个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];
}
Run Code Online (Sandbox Code Playgroud)

我使用以下方法设置进度变量:

- (void)setProgress:(float)progress {
    _progress = progress;
    [self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)

然后我在我的主视图控制器中调用附加方法后,将上述类的UIView分配给我的故事板:

- (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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*cel 5

usleep(50000) 阻止线程

NSTimer而是使用更新progressView.

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


- (void)updateProgressView {
    // Update the progress
  }
}
...
Run Code Online (Sandbox Code Playgroud)