直播间小萌图的实现

2017-09-06  本文已影响0人  ___Lynn

使用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];

}

上一篇 下一篇

猜你喜欢

热点阅读