【问题标题】:Auto layout constraint and CALayer issue自动布局约束和 CALayer 问题
【发布时间】:2015-02-11 15:30:55
【问题描述】:

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

- (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];

但结果是我看不到图像。我正在使用正确的自动布局约束,如果我不使用上面的圆角方法,我会看到图像。但是如果我使用它,我看不到图像。

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

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

- (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;
}

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

【问题讨论】:

    标签: ios ios7 ios8 calayer cashapelayer


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,您希望您的 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-09
        • 2015-04-06
        • 2018-07-06
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-20
        相关资源
        最近更新 更多