美文网首页
iOS 要实现渐变的怪现象

iOS 要实现渐变的怪现象

作者: LV大树 | 来源:发表于2021-04-29 18:40 被阅读0次

    众所周知的原因,ios要想实现渐变的话,需要先知道渐变容器的尺寸和设置渐变layer的大小。

    由于我们经常会使用到动态计算高度。这个时候就不好处理了。

    今天,第一次体会到, layoutIfNeeded的威力。

    当Masonry 布局完毕后,调用上面的layoutIfNeeded。则可以轻松得到布局控件的frame,从而为我们添加渐变提供先置条件。
    解决问题。

    -(void)layoutSubviews{
        NSLog(@"%s",__func__);
        return;
    
        if (self.greatAgent) {
            if (bgViewLayer) {
                [bgViewLayer removeFromSuperlayer];
            }
            self.bgView.backgroundColor = [UIColor clearColor];
            CAGradientLayer *gl = [CAGradientLayer layer];
            gl.frame = CGRectMake(0, 0, mScreenWidth, mScreenHeight);// self.bgView.bounds;// self.agentTagsBGView.bounds;// CGRectMake(96,101,189,18);
            gl.startPoint = CGPointMake(0, 0);
            gl.endPoint = CGPointMake(0, 1);
            gl.colors = @[(__bridge id)HexRGB(0xF9F0E4).CGColor,(__bridge id)HexRGB(0xEED9B8).CGColor];
            gl.locations = @[@(0), @(1.0f)];
            bgViewLayer = gl;
            [self.bgView.layer addSublayer:bgViewLayer];
            self.bgView.layer.cornerRadius = 8;
            self.bgView.layer.masksToBounds = YES;
        }else{
            self.bgView.backgroundColor = [UIColor whiteColor];
        }
    
    }
    

    此乃关键,有兴趣详细了解这个api吧。

        [self layoutIfNeeded];
    

    后续:还是有bug。不过可以先给一个大概的尺寸。

    ///////////////
    书接上一会
    目前使用延时
    听说masonry布局不是立即生效,涉及到runloop?
    所以最后使用了延时处理。结果OK。

    相关文章

      网友评论

          本文标题:iOS 要实现渐变的怪现象

          本文链接:https://www.haomeiwen.com/subject/qviwrltx.html