美文网首页
直播间小萌图的实现

直播间小萌图的实现

作者: ___Lynn | 来源:发表于2017-09-06 16:52 被阅读0次

使用UIImageView加上CAAnimation实现

小萌图动画由三个部分组成:

 1.改变大小动画

2.改变位置动画

3.改变透明度动画

下面是实现方式

新建一个UIImageView类

这里是在init方法里初始化的动画

- (instancetype)initWithImage:(UIImage*)image withStartPoint:(CGPoint)point withXlength:(NSInteger)xlengthwithYlength:(double)ylength byDuration:(NSInteger)durationTime{

if(self= [superinitWithImage:image]) {

//位置移动动画

CAKeyframeAnimation*keyAnima_group2=[CAKeyframeAnimationanimationWithKeyPath:@"position"];

//动画的速度变化 这里为线性  匀速的

keyAnima_group2.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];

keyAnima_group2.duration= durationTime; //动画时间

keyAnima_group2.removedOnCompletion=NO;//fillMode生效 必须设为NO

keyAnima_group2.fillMode=kCAFillModeRemoved;//动画开始前及结束后 动画都移除

//使用路径来设置位置动画

CGPathRefcurvedPath = [selfheartPath:pointwithXlength:xlengthwithYlength:ylength];

keyAnima_group2.path= curvedPath;

CGPathRelease(curvedPath);

//缩放动画:

CABasicAnimation*theAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.scale"];

theAnimation.duration=durationTime;

theAnimation.fromValue= [NSNumbernumberWithFloat:0.7];

theAnimation.toValue= [NSNumbernumberWithFloat:1.2];

theAnimation.removedOnCompletion=NO;

theAnimation.fillMode=kCAFillModeRemoved;

//透明度动画

CABasicAnimation*opacityAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];

opacityAnimation.fromValue= [NSNumbernumberWithFloat:1.0];

opacityAnimation.toValue= [NSNumbernumberWithFloat:0];

opacityAnimation.duration=durationTime;

opacityAnimation.removedOnCompletion=NO;

opacityAnimation.fillMode=kCAFillModeForwards;//动画结束保留结束状态   此处设该值是因为动画开始后APP切后台会造成动画不被移除 保留透明状态可解决

[self.layer addAnimation:opacityAnimationforKey:@"animateTransform.opacity"];//额外增加一次动画 防止动画结束的后会在imageView初始化的位置出现

//组合两个动画

CAAnimationGroup* g = [CAAnimationGroupanimation];

g.animations=@[keyAnima_group2,theAnimation,opacityAnimation];

g.duration= durationTime;

g.removedOnCompletion=NO;

g.fillMode=kCAFillModeRemoved;

g.beginTime=CACurrentMediaTime();

g.delegate=self;

[self.layeraddAnimation:gforKey:@"g"];

self.animationGroup= g;

}

return self;

}

然后是位移动画path生成部分

#pragma mark private mothed

- (CGPathRef)heartPath:(CGPoint)point withXlength:(NSInteger)xlengthwithYlength:(double)ylength

{

int y =8+(arc4random() %2);//y轴移动次数 

int yHeight = point.y/8;//每次y轴移动高度

int direction =arc4random() %4;//用于x轴移动方向判断

CGMutablePathRefcurvedPath =CGPathCreateMutable();//使用UIBezierPath,路径数值不正常,超出父视图范围,有兴趣的可以试试

CGPathMoveToPoint(curvedPath,NULL, point.x, point.y);//起始位置

for(inti =0;i<y;i++){

if(point.y-yHeight-yHeight*i <=0) {//超过Y轴最大范围就停止

break;

}

xNSIntegerxWidth = (arc4random() % xlength);//每次x轴移动距离

if(direction%2==0) {//方向随机

xWidth = -xWidth;

}

CGPathAddLineToPoint(curvedPath,NULL,point.x+xWidth, point.y-yHeight-yHeight*i);

}

return curvedPath;

}

然后在animation delegate里移除layer层动画

- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag {

if(flag) {

[self.layer removeAllAnimations];

}

}

外面调用时

intimageNum = (arc4random() %7);

UIImage*image = [UIImageimageNamed:[NSStringstringWithFormat:@"小萌图-%d",imageNum]];//随机图片

heartImageView*animation = [[heartImageView alloc]initWithImage:image withStartPoint:CGPointMake(CGRectGetWidth(self.frame) /2,CGRectGetHeight(self.frame) - image.size.height/2) withXlength:10 withYlength:50 byDuration:3];

[self addSubview:animation];

}

相关文章

  • 直播间小萌图的实现

    使用UIImageView加上CAAnimation实现 小萌图动画由三个部分组成: 1.改变大小动画 2.改变位...

  • 注意力分布图

    《直播间注意力分布图》,由意靠哥发布。此图非常清晰,直观的反映出一个直播间的场观人数,在线人数这一判断直播间品质的...

  • 直播标题攻略大全

    做直播一定要明白,直播间的流量与标题、封面图有很大关系,只有你的标题和封面图足够吸引用户,用户才会进入你的直播间。...

  • 小匠直播操作手册

    今天就来给大家说一下如何去操作小匠直播间~ 1、在微信搜索小匠直播间小程序,点击进入直播间 2、点击立即创建直播按...

  • 百万直播间: 性感的卖萌

  • 小程序开发进阶:如何实现直播连麦

    我们上周做了一场免费在线直播课,声网Agora 研发工程师张乾泽分享了小程序直播组件的特点、实现小程序间连麦的方法...

  • 萌字卡片

    临摹了一张小图~萌萌的字吸引我~

  • iOS重拾直播系列-直播间礼物效果

    之前写过直播间的礼物效果的文章iOS开发 - Lottie实现直播间礼物特效iOS开发-SVGA Animatio...

  • 直播间小乐趣

    最近周末常逛逛TB直播间,觉得还是能淘到一些好东西。TB的智能推荐不知道什么算法,似乎和品牌有点关系,往...

  • 小匠直播操作手册

    今天就来教大家怎么使用小匠直播吧~ 1.在微信搜索小匠直播间小程序,点击进入直播间 2.点击立即创建直播按钮,填写...

网友评论

      本文标题:直播间小萌图的实现

      本文链接:https://www.haomeiwen.com/subject/cssujxtx.html