UIBezierPath不起作用

时间:2014-06-24 04:28:18

标签: ios xcode uibezierpath

在想到发布这个问题之前我想到了我已经花了几个小时来处理这段小代码...所以我在两个不同的类中写了两个不同的代码片段,两个都用来剪掉特定的形状。但由于某种原因,其中只有一个工作!?!?我绝对不知道为什么会这样,因为它们具有完全相同的结构。

以下是WORKS的片段。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor blackColor];
        UIBezierPath *arrowPath = [UIBezierPath bezierPath];
        CGPoint start = CGPointMake(self.frame.size.width - 2, self.frame.size.height / 2);
        [arrowPath moveToPoint:start];
        [arrowPath addLineToPoint:CGPointMake(7, 7)];
        [arrowPath addLineToPoint:CGPointMake(2, 7)];
        [arrowPath addLineToPoint:CGPointMake(self.frame.size.width - 7, self.frame.size.height/2)];
        [arrowPath addLineToPoint:CGPointMake( 2, self.frame.size.height - 7)];
        [arrowPath addLineToPoint:CGPointMake(7, self.frame.size.height - 7)];
        [arrowPath addLineToPoint:start];
        [arrowPath closePath];
        UIView *arrow = [[UIView alloc]initWithFrame:self.frame];
        arrow.backgroundColor = [UIColor whiteColor];
        CAShapeLayer *mask = [CAShapeLayer layer];
        mask.frame = self.frame;
        mask.backgroundColor = (__bridge CGColorRef)([UIColor clearColor]);
        mask.path = arrowPath.CGPath;
        arrow.layer.mask = mask;
        [self addSubview:arrow];
        }
    return self;
}

以下是仅部分有效的代码。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIView *roundedRectView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 20)];
        roundedRectView.backgroundColor = [UIColor clearColor];
        [self addSubview:roundedRectView];

        UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:roundedRectView.frame cornerRadius:10];
        UIView *rectangle = [[UIView alloc]initWithFrame:roundedRectView.frame];
        rectangle.backgroundColor = [UIColor blackColor];
        CAShapeLayer *mask = [CAShapeLayer layer];
        mask.frame = rectangle.frame;
        mask.path = roundedRectPath.CGPath;
        mask.backgroundColor = (__bridge CGColorRef)([UIColor clearColor]);
        rectangle.layer.mask = mask;
        rectangle.alpha = 0.8;
        [roundedRectView addSubview:rectangle];

        UIView *rectangleTwo = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20)];
        rectangleTwo.backgroundColor = [UIColor redColor];
        UIBezierPath *trianglePath = [UIBezierPath bezierPath];
        CGPoint start = CGPointMake(rectangleTwo.frame.size.width / 2 - 15, 0);
        [trianglePath moveToPoint:start];
        [trianglePath addLineToPoint:CGPointMake(start.x + 20, start.y)];
        [trianglePath addLineToPoint:CGPointMake(start.x + 10, start.y + 20)];
        [trianglePath addLineToPoint:start];
        [trianglePath closePath];
        CAShapeLayer *triangleMask = [CAShapeLayer layer];
        triangleMask.frame = rectangleTwo.frame;
        triangleMask.path = trianglePath.CGPath;
        rectangleTwo.layer.mask = triangleMask;
        [self addSubview:rectangleTwo];
    }
    return self;
}

使用CGRectMake(0,0,80,60)分配此讲话气泡,屏幕上只显示圆角矩形,但三角形无处可见。

有人能指出这段代码有什么问题吗?感谢任何帮助,谢谢!!

1 个答案:

答案 0 :(得分:1)

问题解决了! 原来我的triangleMask.frame没有正确定义。而不是triangleMask.path = rectangleTwo.frame我应该使用triangleMask.frame = CGRectMake(0, 0, rectangleTwo.frame.size.width, rectangleTwo.frame.size.height);代替! CAShapeLayer的原点是相对于你要放置它的形状的原点!