我创建了一个自定义进度栏,该进度栏继承了UIView并实现了drawRect。我设法在整个视图上绘制一个渐变。但是,我想绘制几个不同的渐变,每个渐变都位于不同的位置。如何在我的视图中将CGContextDrawLinearGradient
限制为较小的rect?
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGPoint topCenter = CGPointMake(start + (CGRectGetMidX(currentBounds)/currentBounds.size.width), 0.0f);`
CGPoint midCenter = CGPointMake(start + (CGRectGetMidX(currentBounds)/currentBounds.size.width), currentBounds.size.height);
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);
start = start + (values[i] / currentBounds.size.width);
CGGradientRelease(glossGradient);
}
最佳答案
您可以使用CGContectClipToRect
限制绘图区域
然后对每个渐变执行以下操作:
CGContextSaveGState(currentContext);
CGContextClipToRect(theRect); // theRect should be the area where you want to draw the gradient
... // gradient drawing code
CGContextRestoreGState(currentContext);