1. 程式人生 > >ios開發之--CAKeyframeAnimation的詳細用法

ios開發之--CAKeyframeAnimation的詳細用法

edi true nco 創建 rds repeat oval iat ima

簡單的創建一個帶路徑的動畫效果,比較粗糙,不過事先原理都是一樣的,

代碼如下:

1,創建動畫所需的view

-(void)creatView
{
    moveView = [UIView new];
    
    moveView.backgroundColor = [UIColor purpleColor];
    
    moveView.frame = CGRectMake(0, 0, 30, 30);
    
    [self.view addSubview:moveView];
}

2,動畫的實現:

方法一:

-(void)startAnimation
{
//    方法一 用法1? Value方式
    
//創建動畫對象 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //設置value NSValue *value1=[NSValue valueWithCGPoint:CGPointMake(100, 100)]; NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(200, 100)]; NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(200
, 200)]; NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(100, 200)]; NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(100, 300)]; NSValue *value6=[NSValue valueWithCGPoint:CGPointMake(200, 400)]; animation.values=@[value1,value2,value3,value4,value5,value6];
//重復次數 默認為1 animation.repeatCount=MAXFLOAT; //設置是否原路返回默認為不 animation.autoreverses = YES; //設置移動速度,越小越快 animation.duration = 4.0f; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; //給這個view加上動畫效果 [moveView.layer addAnimation:animation forKey:nil]; }

方法二:

//    用法2?  Path方式
    
    //創建動畫對象
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //創建一個CGPathRef對象,就是動畫的路線
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    //自動沿著弧度移動
    
    CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 200, 200, 100));
    
    //設置開始位置
    
    CGPathMoveToPoint(path,NULL,100,100);
    
    //沿著直線移動
    
    CGPathAddLineToPoint(path,NULL, 200, 100);
    
    CGPathAddLineToPoint(path,NULL, 200, 200);
    
    CGPathAddLineToPoint(path,NULL, 100, 200);
    
    CGPathAddLineToPoint(path,NULL, 100, 300);
    
    CGPathAddLineToPoint(path,NULL, 200, 400);
    
    //沿著曲線移動
    
    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
    
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
    
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
    
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
    
    animation.path=path;
    
    CGPathRelease(path);
    
    animation.autoreverses = YES;
    
    animation.repeatCount=MAXFLOAT;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    animation.duration = 4.0f;
    
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    animation.delegate=self;
    
    [moveView.layer addAnimation:animation forKey:nil];
}

按照上面的方法,即可實現一個動畫,參照上面的邏輯可以實現添加購物車,刪除等帶路徑的動畫!

下面附一個添加購物車的動畫效果:

1,創建動畫view

-(void)relodata
{
    self.addCartImg = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, self.view.frame.size.height+10, 30, 30)];
    self.addCartImg.hidden = true;
    [self.view addSubview:self.addCartImg];
    self.addCartImg.image = [UIImage imageNamed:@"3.jpg"];
}

2,具體動畫的實現:

- (IBAction)clickAddShopCartBtn:(id)sender {
    
    self.addCartImg.hidden = false;
//    創建動畫對象
    CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//    創建一個CGPathRef對象,就是動畫的路線
    CGMutablePathRef path = CGPathCreateMutable();
//    設置開始位置
    CGPathMoveToPoint(path, NULL, self.addCartImg.layer.position.x-40, self.addCartImg.layer.position.y-40);//移動到起始點
//    沿著路徑添加四曲線點移動
    CGPathAddQuadCurveToPoint(path, NULL, 100, 100, self.view.frame.size.width, 0);
    keyframeAnimation.path = path;
    keyframeAnimation.delegate = self;
    CGPathRelease(path);
    keyframeAnimation.duration = 0.7;
    [self.addCartImg.layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
    
//    旋轉動畫
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 50;
    
//    為addCartImg添加旋轉動畫
    [self.addCartImg.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

點擊按鈕,addCartImg會做一個旋轉操作,並按照規定的路徑進行移動,從而完成一個動畫!

ios開發之--CAKeyframeAnimation的詳細用法