主页 iOS Video Loading Animation
Post
Cancel

iOS Video Loading Animation

Preface

I’ve spent the past few days wrestling with the open-source TikTok (Douyin) demo. Today I’m sharing the video loading animation used in TikTok or Kuaishou.

Here’s the finished result:

How It Works

First, I create a view:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@interface ViewController ()
@property (nonatomic, strong) UIView *playLoadingView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //init player status bar
    self.playLoadingView = [[UIView alloc]init];
    self.playLoadingView.backgroundColor = [UIColor whiteColor];
    [self.playLoadingView setHidden:YES];
    [self.view addSubview:self.playLoadingView];
    
    //make constraintes
    [self.playLoadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.mas_equalTo(1.0f); //宽 1 dp
        make.height.mas_equalTo(0.5f); //高 0.5 dp
    }];
    
    [self startLoadingPlayAnimation:YES]; //调用动画代码
}

As you can see, what we actually create is a view 1 pt wide and 0.5 pt tall.

Next, the animation implementation code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)startLoadingPlayAnimation:(BOOL)isStart {
    if (isStart) {
        self.playLoadingView.backgroundColor = [UIColor whiteColor];
        self.playLoadingView.hidden = NO;
        [self.playLoadingView.layer removeAllAnimations];
        
        CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc] init];
        animationGroup.duration = 0.5;
        animationGroup.beginTime = CACurrentMediaTime() + 0.5;
        animationGroup.repeatCount = MAXFLOAT;
        animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        
        CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
        scaleAnimation.keyPath = @"transform.scale.x";
        scaleAnimation.fromValue = @(1.0f);
        scaleAnimation.toValue = @(1.0f * ScreenWidth);
        
        CABasicAnimation *alphaAnimation = [CABasicAnimation animation];
        alphaAnimation.keyPath = @"opacity";
        alphaAnimation.fromValue = @(1.0f);
        alphaAnimation.toValue = @(0.5f);
        
        [animationGroup setAnimations:@[scaleAnimation, alphaAnimation]];
        [self.playLoadingView.layer addAnimation:animationGroup forKey:nil];
    } else {
        [self.playLoadingView.layer removeAllAnimations];
        self.playLoadingView.hidden = YES;
    }
}

That’s it — just those few lines and you’re done.

Actually, the core is only 4 lines of code:

1
2
3
4
CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
scaleAnimation.keyPath = @"transform.scale.x";
scaleAnimation.fromValue = @(1.0f);
scaleAnimation.toValue = @(1.0f * ScreenWidth);

The key is scaleAnimation.keyPath = @"transform.scale.x"; — here we want to scale along the x-axis.

The scale value goes from 1 to screen width — of course, you can control how large the value gets.

If you use @"transform.scale.y", it scales along the Y-axis.

And if you write @"transform.scale", it scales on both X and Y — give it a try.

Summary

The animation technique in this post is scaling via transform.scale.y: scaling the layer from a single point produces the loading effect.

Finally, here’s the demo

Thanks for your support

该博客文章由作者通过 CC BY 4.0 进行授权。