[英]Can I use UIBezierPath to draw a shadow image around a UITableView section?
I have a shadow image that I would like to draw around the outer edge of a grouped UITableView section. 我有一个阴影图像,我想在分组的UITableView部分的外边缘绘制。 This is the image:
这是图像:
I can get the UIBezierPath that represents the rect I want to draw around, but I can't figure out how to repeat the image along the route of the rect. 我可以得到代表我想要绘制的矩形的UIBezierPath,但我无法弄清楚如何沿着rect的路径重复图像。 So far it just fills the rect with the image:
到目前为止它只是用图像填充矩形:
UIImage *patternImg = [UIImage imageNamed:@"cellShadow"];
UIColor *fill = [UIColor colorWithPatternImage:patternImg];
[fill setFill];
CGRect aSectRect = [self rectForSection:0];
UIBezierPath *aSectPath = [self createRoundedPath:aSectRect];
[aSectPath fill];
Is this possible? 这可能吗? What do I need to do?
我需要做什么?
Unfortunately, there's no way to have UIBezierPath use an image as a "brush" which is essentially what you'd want. 不幸的是,没有办法让UIBezierPath将图像用作“刷子”,这本质上就是你想要的。 But you can have CoreGraphics draw a shadow for you:
但是你可以让CoreGraphics为你画一个阴影:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
// Draw your shape here
Now if you draw just one shape it will get a shadow. 现在,如果你只绘制一个形状,它将获得一个阴影。 But if you draw more shapes, each will get its own shadow which might not be what you want.
但是如果你绘制更多的形状,每个都会得到自己的影子,这可能不是你想要的。 The solution is called a transparency layer, which is not related to CALayers or something but is just some kind of "grouping" in CoreGraphics:
该解决方案称为透明层,与CALayers无关,但只是CoreGraphics中的某种“分组”:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
CGContextBeginTransparencyLayer(context, NULL);
// Draw your shapes here.
CGContextEndTransparencyLayer(context);
Between the CGContextBeginTransparencyLayer
and CGContextEndTransparencyLayer
calls the shadow is disabled. 在
CGContextBeginTransparencyLayer
和CGContextEndTransparencyLayer
调用阴影被禁用。 After the CGContextEndTransparencyLayer
call, the shadow is applied to everything that has been drawn between begin and end as if it were just one shape. 在
CGContextEndTransparencyLayer
调用之后,阴影将应用于在开始和结束之间绘制的所有内容,就好像它只是一个形状一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.