使用uibezierpath剪切UIView。 BezierPath似乎没有完全应用

时间:2013-10-03 09:06:56

标签: ios uiview uibezierpath

我必须为UIView添加一个带有木质纹理的边框。为了做到这一点,我想到了以下解决方案:

  • 我有原始的UIView(说uiViewA)
  • 我创建了另一个与第一个相同大小的UIView(比如uiViewB)
  • 我创建了一个宽度为8px的bezier路径
  • 创建路径后,我将其应用于uiViewB
  • 我将uiViewB添加到uiViewA的子视图

应用贝塞尔曲线路径的代码如下:

UIView* uiViewB = [[UIView alloc] initWithFrame:uiViewA.bounds];
UIImage* wood = [UIImage imageNamed:@"texture_wood"];
[uiViewB setBackgroundColor:[UIColor colorWithPatternImage:wood]];

// creation of the bezier path
UIBezierPath* borderPath = ... ;
[borderPath setLineWidth:8.0];
[borderPath moveToPoint:CGPointMake(0.0, 0.0)];
borderPath addLineToPoint:...
borderPath addArcWithCenter:...
borderPath addLineToPoint:...
borderPath addArcWithCenter:...
borderPath addLineToPoint:...

CAShapeLayer* borderMaskLayer = [CAShapeLayer layer];
[borderMaskLayer setFrame:uiViewA.bounds];
borderMaskLayer.path = [borderPath CGPath];
uiViewB.layer.mask = borderMaskLayer;
[uiViewB.layer setMasksToBounds:YES];

我想获得以下结果:

enter image description here

但我得到的结果如下:

enter image description here

你是否知道为什么bezier路径似乎没有以正确的方式应用?

1 个答案:

答案 0 :(得分:2)

从您所拥有的代码中看起来您正在使用用作蒙版的形状图层的默认填充和描边,因此路径已填充并且您将获得您所看到的结果。

您要做的是设置清晰的填充颜色并设置一些随机不透明的笔触颜色。然后,您必须设置适当的线宽,线帽,线圆等,以配置笔划的外观。