iOS 骨架加载动画

如何解决iOS 骨架加载动画

我正在为 iOS 应用程序添加骨架加载。我有一个 UICollectionView,其中每个单元格都应该在加载数据时显示“加载”状态。为了处理这种状态,将使用骨架加载技术。

对于这种情况,我只是获取标签和图像,然后向它们添加一个子图层,即灰色子图层,由每个标签或图像的边界遮蔽。这工作正常。

问题是我还想要一个“光泽”或“微光”动画,从左到右穿过每个视图并重复。但我无法让动画工作。它只是看起来像这样,没有任何动画。第二项颜色较浅,因为它被禁用,否则它们都是相同的颜色。但是一切看起来都很好,只是应该有动画。

enter image description here

代码

这是我拥有的代码。同样,问题是动画没有发生。应该有一个 CAGradientLayer 从左侧开始,在视野之外,并在右侧进行动画处理,直到它脱离视野。然后一切都会重演。

@interface MyCollectionViewCell ()

/**
 * If this is YES,then the skeleton loading layers and animation need to be shown.
 * If this is NO,then the normal view should be shown. No skeleton loading layers should be shown.
 */
@property (assign,nonatomic,getter=isLoading) BOOL loading;

@end

@implementation MyCollectionViewCell

//... initialization and other logic

/**
 * This finds the direct subviews of the contentView that are either labels or images.
 */
- (NSArray<__kindof UIView *> *)loadableSubviews {
    NSMutableArray<UIView *> *views = [[NSMutableArray alloc] init];
    for (UIView *view in self.contentView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            [views addobject:view];
        } else if ([view isKindOfClass:[UIImageView class]]) {
            [views addobject:view];
        }
    }
    return views;
}

- (void)setLoading:(BOOL)loading {
    self->_loading = loading;
    
    NSArray<__kindof UIView *> *loadingViews = [self loadableSubviews];
    
    if (loading) {
        //display the skeleton loading layers

        UIColor *backgroundColor = [UIColor colorWithRed:(210.0/255.0) green:(210.0/255.0) blue:(210.0/255.0) alpha:1.0];
        UIColor *highlightColor = [UIColor colorWithRed:(235.0/255.0) green:(235.0/255.0) blue:(235.0/255.0) alpha:1.0];
        CALayer *skeletonLayer = [CALayer layer];
        skeletonLayer.backgroundColor = backgroundColor.CGColor;
        skeletonLayer.name = @"skeletonLayer";
        skeletonLayer.anchorPoint = CGPointZero;
        skeletonLayer.frame = UIScreen.mainScreen.bounds;
        
        for (UIView *loadingView in loadingViews) {
            CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
            gradientLayer.colors = @[backgroundColor,highlightColor,backgroundColor];
            gradientLayer.startPoint = CGPointMake(0.0,0.5);
            gradientLayer.endPoint = CGPointMake(1.0,0.5);
            gradientLayer.frame = UIScreen.mainScreen.bounds;
            gradientLayer.name = @"skeletonGradient";
            
            loadingView.layer.mask = skeletonLayer;
            [loadingView.layer addSublayer:skeletonLayer];
            [loadingView.layer addSublayer:gradientLayer];
            loadingView.clipsToBounds = YES;
            
            CGFloat width = UIScreen.mainScreen.bounds.size.width;
            
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
            animation.duration = 3.0;

            //Todo: is there maybe something wrong with the y coordinate here?
            animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(-width,gradientLayer.frame.origin.y)];
            animation.tovalue = [NSValue valueWithCGPoint:CGPointMake(width,gradientLayer.frame.origin.y)];
            animation.repeatCount = CGFLOAT_MAX;
            animation.autoreverses = NO;
            animation.fillMode = kCAFillModeForwards;
            
            [gradientLayer addAnimation:animation forKey:@"gradientShimmer"];
        }
    } else {
        //remove the skeleton loading layers

        for (UIView *loadingView in loadingViews) {
            for (NSInteger x = loadingView.layer.sublayers.count - 1; x >= 0; x--) {
                CALayer *sublayer = loadingView.layer.sublayers[x];
                if ([sublayer.name isEqualToString:@"skeletonLayer"] || [sublayer.name isEqualToString:@"skeletonGradient"]) {
                    [sublayer removeFromSuperlayer];
                }
            }
        }
    }
}

@end

调试

我在将动画添加到图层后添加一个断点并检查了 LLDB 中的 gradientLayer,它显示动画已附加到图层:

(lldb) po gradientLayer
<CAGradientLayer:0x280964380; name = "skeletonGradient"; position = CGPoint (187.5 406); bounds = CGRect (0 0; 375 812); allowsGroupOpacity = YES; name = skeletonGradient; endPoint = CGPoint (1 0.5); startPoint = CGPoint (0 0.5); colors = (
    "UIExtendedSRGBColorSpace 0.823529 0.823529 0.823529 1","UIExtendedSRGBColorSpace 0.921569 0.921569 0.921569 1","UIExtendedSRGBColorSpace 0.823529 0.823529 0.823529 1"
); animations = [gradientShimmer=<CABasicAnimation: 0x2809645e0>]>

但由于某种原因,它似乎没有运行。

问题

  1. 有人知道为什么动画不起作用吗?
  2. 有没有更好的方法来做到这一点?
  3. 特别是,有没有更好的方法来移除加载状态?有没有更好的方法去除这些层?

解决方法

您将一组 UIColor 对象设置为 gradientLayer 的 colors 属性。相反,您必须使用 CGColors,请参阅此处的 Apple 文档:

一组 CGColorRef 对象,用于定义每个渐变色标的颜色。可动画。

https://developer.apple.com/documentation/quartzcore/cagradientlayer/1462403-colors?language=objc

所以这意味着您应该按如下方式更改分配:

gradientLayer.colors = @[(id)backgroundColor.CGColor,(id)highlightColor.CGColor,(id)backgroundColor.CGColor];

测试

在小动画GIF中,灰度渐变没有实际效果好,但可以看到它给出了预期的结果

Test

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?