如何在物镜C中从矩形视图中截取三角形视图?

时间:2018-11-06 03:08:15

标签: objective-c uiview uibezierpath cashapelayer

我想查看下图所示的图像:- desired view 我正在使用UIBezierPath和CAShapeLayer将以下代码存档:-

     UIBezierPath *path = [[UIBezierPath alloc] init];
//    [[UIColor blackColor] setStroke];
//    [[UIColor redColor] setFill];
    [path moveToPoint:CGPointMake(_pendingView.frame.origin.x, _pendingView.frame.origin.y)];
    [path addLineToPoint:CGPointMake(_pendingView.frame.origin.x+15, _pendingView.frame.origin.y+15)];
    [path addLineToPoint:CGPointMake(_pendingView.frame.origin.x, _pendingView.frame.origin.y+30)];
    [path closePath];

//    [path fill ]; 
//    [path stroke];
    CAShapeLayer *shapeLayer = [CAShapeLayer new];
    shapeLayer.frame = _pendingView.bounds;
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;

    self.pendingView.layer.mask = shapeLayer;
   // [self.pendingView.layer addSublayer:shapeLayer];

但是我无法实现。视图显示时没有剪切和视图的背景颜色。

PS:-未决视图是一个视图。

按照@inderJit的建议,我更正了我的代码,但是我收到了这个output image,但上面没有附上。查看引起问题的背景色。我该如何实现?

1 个答案:

答案 0 :(得分:0)

在您的代码中,您正在使用_pendingView,即您的UIView,绘制路径的原点,但是图层将放置在_pendingView内,因此,您将无法获得正确的输出。 / p>

为进一步说明,您可以浏览以下代码

UIBezierPath *path = [[UIBezierPath alloc] init];
//    [[UIColor blackColor] setStroke];
//    [[UIColor redColor] setFill];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(15, _vwArrow.frame.size.height/2)];
[path addLineToPoint:CGPointMake(0, _vwArrow.frame.size.height)];
[path closePath];

//    [path fill ];
//    [path stroke];
CAShapeLayer *shapeLayer = [CAShapeLayer new];
shapeLayer.frame = _vwArrow.bounds;
shapeLayer.path = path.CGPath;
shapeLayer.fillColor = self.view.backgroundColor.CGColor;
//    shapeLayer.strokeColor = [UIColor blackColor].CGColor;

//    _vwArrow.layer.masksToBounds = TRUE;
[_vwArrow.layer addSublayer:shapeLayer];
[self.view setNeedsDisplay];