1

我有一个阴影图像,我想在分组的 UITableView 部分的外边缘绘制。这是图像:

在此处输入图像描述

我可以获得代表我想要绘制的矩形的 UIBezierPath,但我不知道如何沿着矩形的路径重复图像。到目前为止,它只是用图像填充矩形:

UIImage *patternImg = [UIImage imageNamed:@"cellShadow"];
UIColor *fill = [UIColor colorWithPatternImage:patternImg];
[fill setFill];
CGRect aSectRect = [self rectForSection:0];
UIBezierPath *aSectPath = [self createRoundedPath:aSectRect];
[aSectPath fill];

这可能吗?我需要做什么?

4

1 回答 1

3

不幸的是,没有办法让 UIBezierPath 使用图像作为“画笔”,这基本上是你想要的。但是您可以让 CoreGraphics 为您绘制阴影:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
// Draw your shape here

现在如果你只画一个形状,它会得到一个阴影。但是如果你画更多的形状,每个都会得到自己的阴影,这可能不是你想要的。该解决方案称为透明层,它与CALayers或其他东西无关,而只是CoreGraphics中的某种“分组”:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
CGContextBeginTransparencyLayer(context, NULL);
// Draw your shapes here.
CGContextEndTransparencyLayer(context);

CGContextBeginTransparencyLayerCGContextEndTransparencyLayer调用之间,影子被禁用。调用后CGContextEndTransparencyLayer,阴影将应用于在开始结束之间绘制的所有内容,就好像它只是一个形状一样。

于 2013-05-13T18:27:22.100 回答