3

我在我的 UIView 子类中使用这段代码来绘制一个带有渐变填充的圆圈:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShadow (context, CGSizeMake(4,4), 5);
    CGContextBeginPath (context);
    CGContextAddEllipseInRect(context, CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 38, 38));
    CGContextClosePath (context);
    CGContextClip (context);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMaxX(self.bounds)), CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMinY(self.bounds)), 0);
}

圆圈和渐变画得很好,但我看不到阴影。我不确定为什么它不起作用,因为我在不同的视图子类中使用了相同的 CGContextSetShadow 函数并且它运行良好。

注意:在上面的代码中,“梯度”是之前定义的 ivar。

4

1 回答 1

3

A gradient draw doesn't count as a fill; only fills and strokes get shadows. (You may want to report this as a bug.)

As a workaround, fill the path (with solid black), then turn off the shadow, then draw the gradient.

于 2010-02-15T03:42:01.230 回答