iOS清除UIView并重绘drawRect:

时间:2015-09-25 06:05:51

标签: ios objective-c uiview drawrect

我希望我的自定义视图清除其中的所有内容(子视图,子图层,绘图等),并在按下按钮时再次调用drawRect:,这样就像获得一块干净的石板并再次在其上绘画

现在,我已尝试使用setNeedsDisplay,它只是在不清理以前的工作的情况下完成整个事情。

我的drawRect中的一段代码:

if (_displayYAxisAverageLine) {
    // Check if y axis data are numbers
    for (id yValue in yAxisValues) {
        if (![yValue isKindOfClass:[NSNumber class]]) {
            return;
        }
    }

    NSNumber *average = [yAxisValues valueForKeyPath:@"@avg.self"];

    CGFloat scale = [self scaleForYValue:average];
    CGFloat avgLineY = roundf(graphHeight * scale)+_graphInset;

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, avgLineY)];
    [path addLineToPoint:CGPointMake(contentWidth, avgLineY)];

    CAShapeLayer *avgLine = [CAShapeLayer layer];
    avgLine.path = path.CGPath;
    avgLine.fillColor = [UIColor clearColor].CGColor;
    avgLine.strokeColor = _borderColor.CGColor;
    avgLine.lineWidth = 1.f;
    avgLine.lineDashPattern = @[@2, @2];

    [self.contentView.layer addSublayer:avgLine];
}

0 个答案:

没有答案