我在 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);
}
圆圈和渐变画得很好,但我看不到阴影。我不确定为什么它不起作用,因为我在不同的 View 子类中使用了相同的 CGContextSetShadow 函数并且它工作得很好。
注意:在上面的代码中,“gradient”是之前定义的 ivar。
最佳答案
渐变绘制不算填充;只有填充和描边才会有阴影。 (您可能需要report this as a bug。)
作为解决方法,填充路径(使用纯黑色),然后关闭阴影,然后绘制渐变。
关于iphone - CGContextSetShadow 不产生任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2263273/