自动布局约束和CALayer问题

时间:2015-02-11 15:30:56

标签: ios ios7 ios8 calayer cashapelayer

我正在使用此代码使我的顶角变圆。

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(5.0, 5.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

然后在superview方法的initWithFrame中我做到了:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self setMaskTo:imageView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];

但结果却没有看到图像。我正在使用正常工作的自动布局约束,如果我不使用上面的圆角方法,我会看到图像。但如果我使用它,我就不会看到图像。

似乎如果使用我的视图的draw rect回调放置图像,我可以设置圆角。但是在这种情况下,如果视图包含其他视图,它将向所有子视图添加舍入,如果我将循环它:

它当然适用于所有子视图:

- (void)drawRect:(CGRect)rect {
    // Drawing code

    for (UIView *view in self.subviews)
    {
        [self setMaskTo:view byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    }

}

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(5.0, 5.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

那么如何检测图像视图何时有帧,我认为帧开始时为零,CALayer不了解如何与视图交互。

2 个答案:

答案 0 :(得分:1)

发生这种情况是因为您使用框架CGRectZero实例化视图,然后使用该边界作为蒙版,因此视图中的任何内容都不在掩码中,因此不会显示任何内容。

作为一种解决方法,您可以将标记设置为不同于0的UIImageView实例,并在子视图中搜索

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.tag = 111; //or any other number you want

- (void)drawRect:(CGRect)rect {
// Drawing code

for (UIView *view in self.subviews)
{
    if (view.tag==111){[self setMaskTo:view byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}}

}

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                              byRoundingCorners:corners
                                                    cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}

答案 1 :(得分:0)

如果我理解正确,你希望你的UIView里面有一个图像,在顶部有圆角,对吧?然后你可以在layoutSubviews中调整遮罩层的大小。

类似的东西:

- (void)awakeFromNib {
    [super awakeFromNib];

    [self setMaskTo:self byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    self.layer.mask.frame = self.layer.bounds;
}

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners {
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(5.0, 5.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
    view.layer.masksToBounds = YES;
}