推广

iOS-快速生成骨架loading

iseeyu2年前 (2024-02-22)推广145

WX20210426-141749@2x.png

最简单的方式就是创建多个view进行排版,然后自定义view的layer层,在加上动画。直接上代码

代码

static NSInteger maskViewTag = 100111;

@implementation UIView (WBBLDetailSkeletonLoading)

- (void)addBLDetailSkeletonLoadMasking{
    
    UIView *maskView = [[UIView alloc] initWithFrame:self.bounds];
    [maskView setBackgroundColor:[UIColor wb_lightColorWithHex:0xffffff darkColorWithHex:0x000000]];
    [maskView setTag:maskViewTag];
    [self addSubview:maskView];
    
    UIView *titleTopView = [[UIView alloc] initWithFrame:CGRectMake(15, 20, kScreenWidth - 30, 24)];
    titleTopView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    titleTopView.layer.masksToBounds = YES;
    titleTopView.layer.cornerRadius = 3;
    [maskView addSubview:titleTopView];
    
    UIView *titleBottomView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleTopView.frame) + 7, 130, 24)];
    titleBottomView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    titleBottomView.layer.masksToBounds = YES;
    titleBottomView.layer.cornerRadius = 3;
    [maskView addSubview:titleBottomView];
    
    UIView *userHeaderView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleBottomView.frame) + 20, 34, 34)];
    userHeaderView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    userHeaderView.layer.masksToBounds = YES;
    userHeaderView.layer.cornerRadius = 34 / 2.0;
    [maskView addSubview:userHeaderView];
    
    UIView *userNameView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userHeaderView.frame) + 8, CGRectGetMaxY(titleBottomView.frame) + 20, 70, 14)];
    userNameView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    userNameView.layer.masksToBounds = YES;
    userNameView.layer.cornerRadius = 3;
    [maskView addSubview:userNameView];
    
    UIView *userInfoView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userHeaderView.frame) + 8, CGRectGetMaxY(userNameView.frame) + 5.5, 94, 14)];
    userInfoView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    userInfoView.layer.masksToBounds = YES;
    userInfoView.layer.cornerRadius = 3;
    [maskView addSubview:userInfoView];
    
    UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 70 - 15, CGRectGetMaxY(titleBottomView.frame) + 25.5, 70, 24)];
    rightButtonView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    rightButtonView.layer.masksToBounds = YES;
    rightButtonView.layer.cornerRadius = 3;
    [maskView addSubview:rightButtonView];
    
    NSInteger lineMsgNum = 5;
    CGFloat lineMsgViewWidth = kScreenWidth - 30;
    for (NSInteger i=0; i<=lineMsgNum; i++) {
        if (i==lineMsgNum) {
            lineMsgViewWidth = 145;
        }
        UIView *lineMsgView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(userInfoView.frame) + 26 + i*28, lineMsgViewWidth, 17)];
        lineMsgView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
        lineMsgView.layer.masksToBounds = YES;
        lineMsgView.layer.cornerRadius = 3;
        [maskView addSubview:lineMsgView];
    }
    CGFloat bottomLineMsgViewY = CGRectGetMaxY(userInfoView.frame) + 26 + lineMsgNum*28 + 17;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(15, bottomLineMsgViewY + 25.5, kScreenWidth - 30, 229)];
    contentView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    contentView.layer.masksToBounds = YES;
    contentView.layer.cornerRadius = 3;
    [maskView addSubview:contentView];
    
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(contentView.frame) + 11, 167, 24)];
    lastView.backgroundColor = [UIColor wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    lastView.layer.masksToBounds = YES;
    lastView.layer.cornerRadius = 3;
    [maskView addSubview:lastView];

    [self addLoadingAnimationWithSuperView:maskView];
}

- (void)addLoadingAnimationWithSuperView:(UIView *)superView{
    for (UIView *view in superView.subviews) {
        [self startAnimationWithView:view];
    }
}

- (void)dismissBLDetailSkeletonLoadMasking{
    if ([self viewWithTag:100111]) {
        [[self viewWithTag:100111] removeFromSuperview];//移除view
    }
}

- (void)startAnimationWithView:(UIView *)view{
    CAGradientLayer *bgViewLayer = [[CAGradientLayer alloc] init];
    bgViewLayer.backgroundColor = [UIColor clearColor].CGColor;
    bgViewLayer.frame = CGRectMake(-view.bounds.size.width, 0, view.bounds.size.width * 3, view.bounds.size.height);
    bgViewLayer.startPoint = CGPointMake(0, 0.5);
    bgViewLayer.endPoint = CGPointMake(1, 0.5);
    UIColor *startColor = [UIColor  wb_lightColorWithHex:0xf6f8f9 darkColorWithHex:0x171717];
    UIColor *endColor = [UIColor  wb_lightColorWithHex:0xe9ebee darkColorWithHex:0x121212];;
    bgViewLayer.colors = [NSArray arrayWithObjects:(__bridge id)startColor.CGColor,(__bridge id)endColor.CGColor,(__bridge id)startColor.CGColor, nil];
    bgViewLayer.locations = @[@0.25,@0.5,@0.75];
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
    rotationAnimation.fromValue = @[@0.0, @0.0, @.25];
    rotationAnimation.toValue = @[@0.75, @1.0, @1.0];
    rotationAnimation.duration = 3;
    rotationAnimation.repeatCount = 10000;
    rotationAnimation.removedOnCompletion = NO;
    [bgViewLayer addAnimation:rotationAnimation forKey:nil];
    [view.layer addSublayer:bgViewLayer];
}

@end

优势

  • 轻量级,灵活可控,可以快速升级迭代
  • 不用引入三方文件,减小包大小

特别鸣谢

iOS – 骨架屏自动生成方案
iOS-用户体验之骨架屏的实践
CAGradientLayer
CAGradientLayer苹果官网

扫描二维码推送至手机访问。

版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

转载请注明出处https://www.0291.com.cn/post/55862.html

相关文章

能量低的人,容易感到累和疲惫

能量低的人,容易感到累和疲惫

分享一些提升能量的小tips1、饮食清淡(减少湿热、抗炎)少吃辛辣油腻,减少体内湿热淤积,摄入过多糖盐会加重身体炎症,导致身体更虚,所以要减少糖分和盐分的摄入,多喝水,可以加一些金须花,抗炎消炎,清除身体的虚火炎症,让我们的身体恢复正常。2、不吃生冷(保护中焦脾胃)寒凉的食...

百度推广有哪些操作技巧?

百度推广有哪些操作技巧?

百度很烧钱,用户体验也不是很好,前辈们来说说,百度竞价有哪些,给大家省点钱。...

如何制定一份优质的营销策划方案?

我们日常经常听到或遇到,要写一份方案,听起来很高大上,初入圈的伙伴甚至觉得云里雾里,不知从何下手。今天,方案君就给大家一个实战经验分享,写营销策划方案其实就是根据一个模板,把你的这个要做的事情,按照顺序1、2、3、4填好,然后其他人照着这个完整的方案执行就可以了,并不是那么...

巨量千川引流视频技巧!

巨量千川引流视频技巧!

  巨量千川直播带货,想要将更多的精准用户带入到直播间,引流视频起着非常关键词的作用。 巨量千川引流短视频如何制作?怎样打造爆款巨量千川引流短视频呢? 在观看、分析了大量跑量视频之后,总结出来的爆款巨量千川引流视频都具备这些核心特点: 爆款巨量千川引流视频特点1:跑量视频...

研学旅行“营销策略”应注意哪些?

研学旅行“营销策略”应注意哪些?

导语:研学旅行除了独特的产品外,多的宣传也必不可少。小编所在的公司是做智慧研学SaaS系统的,近期部同事在给客户做软件使用培训时,发现大多客户都比较关心拓客营销这一块。“营销”对企业的持续稳定性发展和产品售卖都起着关键的作用。营销策略对企业来说更是必不可少。关于研学旅行的营...

抖音直播公会入驻指南

抖音直播公会入驻指南

  要说现如今人手必备的娱乐APP,抖音必定是当仁不让地排列第一,如今的抖音,已经从单纯的日常分享和娱乐,渗透到了我们衣食住行的方方面面。 基于抖音庞大的流量池,许多人纷纷涌入抖音直播行业,并以此捞到了自己人生当中的第一桶金。 在抖音的娱乐直播领域“人手必入”的平台:抖音直...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片