CGContextSetShadow 不产生任何结果
我在 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 函数并且它工作得很好。
注意:在上面的代码中,“gradient”是之前定义的ivar。
I'm using this code in my UIView subclass to draw a circle with a gradient fill:
- (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);
}
The circle and the gradient draw fine, but I see no shadow. I'm not sure why its not working, because I used the same CGContextSetShadow function in a different view subclass and it worked fine.
NOTE: In the above code, "gradient" is an ivar that was defined previously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
渐变绘制不算填充;只有填充和描边才会有阴影。 (您可能需要将此报告为错误。)
作为解决方法,填写 路径(纯黑色),然后关闭阴影,然后绘制渐变。
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.