CALayer的autolayout

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lqzitongyezu/article/details/52385511

autolayout不适用于layer

这里是这么解决的:重写view的layerClass,view使用auto layout,调用view的layoutSubviews重新绘制layer的边界

这里以shape layer为例子

在view.m中如下实现

+(Class)layerClass{
    return [CAShapeLayer class];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        ((CAShapeLayer *)self.layer).fillColor = [UIColor colorWithRed:0 green:183.0 / 255.0 blue:56.0 / 255.0 alpha:1.0].CGColor;
        ((CAShapeLayer *)self.layer).strokeColor = [UIColor colorWithRed:0 green:183.0 / 255.0 blue:56.0 / 255.0 alpha:1.0].CGColor;
        ((CAShapeLayer *)self.layer).strokeEnd = 0;
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(10, CGRectGetHeight(self.bounds) / 2.0)];
    [bezierPath addLineToPoint:CGPointMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 2.0)];
    ((CAShapeLayer *)self.layer).path = bezierPath.CGPath;
    
    ((CAShapeLayer *)self.layer).lineWidth = CGRectGetHeight(self.bounds);
    
}



阅读更多
换一批

没有更多推荐了,返回首页