前言####
本文是高仿映客項(xiàng)目的續(xù)集篇,如果想了解高仿映客項(xiàng)目的更多詳細(xì)資料可以點(diǎn)擊以下鏈接:
映客源碼下載地址:高仿映客項(xiàng)目源碼
映客系列詳細(xì)解說(shuō)目錄:映客系列詳細(xì)解說(shuō)目錄

點(diǎn)贊和彈幕動(dòng)畫.gif
點(diǎn)贊動(dòng)畫####
點(diǎn)贊動(dòng)畫仔細(xì)觀察不難發(fā)現(xiàn),其實(shí)是一個(gè)組動(dòng)畫,凡是什么復(fù)雜的動(dòng)畫都可以一步一步拆解為簡(jiǎn)單的動(dòng)畫一一實(shí)現(xiàn)
組動(dòng)畫的構(gòu)成:路徑動(dòng)畫和關(guān)鍵幀動(dòng)畫組成
首先點(diǎn)擊點(diǎn)贊按鈕的時(shí)候創(chuàng)建一個(gè)imageView
UIImage *praise = [UIImage imageNamed:@"me_other_followed"];
UIImageView *praiseView = [[UIImageView alloc]initWithImage:praise];
praiseView.frame = CGRectMake(LZBSCREEN__WIDTH * 0.5 - praise.size.width *0.5, LZBSCREEN__HEIGHT, praise.size.width, praise.size.width);
[self.view insertSubview:praiseView belowSubview:self.view];
繪制這個(gè)imageView移動(dòng)的路徑
//動(dòng)畫初始值
CGFloat animationH = LZBSCREEN__HEIGHT * 0.5;
//繪制動(dòng)畫路徑
CGFloat offset = 150;
UIBezierPath *animationPath = [UIBezierPath bezierPath];
CGPoint startPoint = praiseView.center;
CGPoint endPoint = CGPointMake(praiseView.center.x, LZBSCREEN__HEIGHT * 0.5);
CGPoint controlPoint1 = CGPointMake(praiseView.center.x -arc4random_uniform(offset) ,LZBSCREEN__HEIGHT - animationH/4);
CGPoint controlPoint2 = CGPointMake(praiseView.center.x +arc4random_uniform(offset), LZBSCREEN__HEIGHT - animationH*3/4);
[animationPath moveToPoint:startPoint];
[animationPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
//關(guān)鍵幀動(dòng)畫,實(shí)現(xiàn)整體圖片位移
CGFloat duration = 3.0;
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.path = animationPath.CGPath;
keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
keyFrameAnimation.duration = duration ;//往上飄動(dòng)畫時(shí)長(zhǎng),可控制速度
實(shí)現(xiàn)imageView放大縮小的關(guān)鍵幀動(dòng)畫
//關(guān)鍵幀動(dòng)畫,實(shí)現(xiàn)整體圖片放大縮小
CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scale.values = @[@1.0,@1.4,@1.8,@2.0,@1.8,@1.6,@1.4,@1.0];
scale.duration = duration;
scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
實(shí)現(xiàn)組動(dòng)畫疊加
//增加組動(dòng)畫
CAAnimationGroup *grounp = [CAAnimationGroup animation];
grounp.animations = @[scale,keyFrameAnimation];
grounp.duration = duration;
[praiseView.layer addAnimation:grounp forKey:@"positionAniamtion"];
最后imageView移出動(dòng)畫區(qū)域 移除imageView
//消失動(dòng)畫
[UIView animateWithDuration:duration animations:^{
praiseView.alpha = 0.0;
} completion:^(BOOL finished) {
[praiseView removeFromSuperview];
}];
文字評(píng)論彈幕動(dòng)畫###

彈幕動(dòng)畫使用的是自定義LZBFlyLabel來(lái)做的
//text彈幕文字 open打開(kāi)隨機(jī)顏色 velocity移動(dòng)速度
- (instancetype)initWithFlyText:(NSString *)text openRandColor:(BOOL)open moveVelocity:(CGFloat)velocity;
具體實(shí)現(xiàn)
- (instancetype)initWithFlyText:(NSString *)text openRandColor:(BOOL)open moveVelocity:(CGFloat)velocity
{
self.offsetY = arc4random()%(200); //200表示隨機(jī)的最大偏移量
self.font = LZBFlyLabel_Font; //設(shè)置字體
self.text = text;
if(self = [super initWithFrame:CGRectMake(getScreenWidth(), self.offsetY, getScreenWidth(), ceil([self getTextSize].height))])
{
self.textColor = open?[self getRandColor]:[UIColor redColor]; //設(shè)置顏色
self.textAlignment = NSTextAlignmentCenter;
self.numberOfLines = 0;
[self addFlyAnimationWithVelocity:velocity]; //增加滾動(dòng)動(dòng)畫
}
return self;
}
增加動(dòng)畫效果
- (void)addFlyAnimationWithVelocity:(CGFloat)velocity
{
LZBWeakSelf(weakSelf);
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:20.0 animations:^{
weakSelf.frame = CGRectMake(-weakSelf.frame.size.width, weakSelf.offsetY, weakSelf.frame.size.width, ceil([self getTextSize].height));
} completion:^(BOOL finished) {
[weakSelf removeFromSuperview];
}];
}