iOS視訊載入動畫
這幾天一直跟開源的抖音demo鬥智鬥勇,今天跟大家分享的是抖音中或者快手中載入視訊的動畫
上圖看成品
實現原理
首先我建立一個檢視
@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]; //呼叫動畫程式碼 }
這裡我們可以看到 我們實際上建立的是一個 1pt寬度 0.5 pt的寬度 的檢視
緊接著動畫實現的程式碼
- (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; } }
完事 就這幾行程式碼 搞定
其實核心的只有4行程式碼
CABasicAnimation *scaleAnimation = [CABasicAnimation animation]; scaleAnimation.keyPath = @"transform.scale.x"; scaleAnimation.fromValue = @(1.0f); scaleAnimation.toValue = @(1.0f * ScreenWidth);
關鍵在 scaleAnimation.keyPath = @"transform.scale.x";
這裡我們要沿著x做縮放
縮放的得值從 1~螢幕寬度 , 當然值多大自己可以控制.
如果 @"transform.scale.y"
則是沿著Y軸縮放
當然 如果寫成 @"transform.scale"
那就X,Y 一起縮放 大家可以試試.
總結
本篇的動畫技巧是 縮放的 transform.scale.y
從一個點 做layer縮放 就會出現 載入效果.
ofollow,noindex">最後附上demo
感謝大家支援