使用UIImageView加上CAAnimation實現(xiàn)
小萌圖動畫由三個部分組成:
?1.改變大小動畫
2.改變位置動畫
3.改變透明度動畫
下面是實現(xiàn)方式
新建一個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生效 必須設(shè)為NO
keyAnima_group2.fillMode=kCAFillModeRemoved;//動畫開始前及結(jié)束后 動畫都移除
//使用路徑來設(shè)置位置動畫
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;//動畫結(jié)束保留結(jié)束狀態(tài) ? 此處設(shè)該值是因為動畫開始后APP切后臺會造成動畫不被移除 保留透明狀態(tài)可解決
[self.layer addAnimation:opacityAnimationforKey:@"animateTransform.opacity"];//額外增加一次動畫 防止動畫結(jié)束的后會在imageView初始化的位置出現(xiàn)
//組合兩個動畫
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軸移動次數(shù)?
int yHeight = point.y/8;//每次y軸移動高度
int direction =arc4random() %4;//用于x軸移動方向判斷
CGMutablePathRefcurvedPath =CGPathCreateMutable();//使用UIBezierPath芋绸,路徑數(shù)值不正常膜廊,超出父視圖范圍蒿柳,有興趣的可以試試
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];
}
}
外面調(diào)用時
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];
}