CABasicAnimation 用法

在网上择要的:

我们可以通过animationWithKeyPath键值对的方式来改变动画
animationWithKeyPath的值:
 
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
 
margin
zPosition
 
backgroundColor
 
cornerRadius
borderWidth

 
bounds
contents

contentsRect
cornerRadius
frame

hidden

mask

masksToBounds
opacity

position

shadowColor

shadowOffset

shadowOpacity
shadowRadius

//移动

- (IBAction)translation:(id)sender {
    CABasicAnimation *traslation = [CABasicAnimation animationWithKeyPath:@"position"];
    traslation.toValue = [NSValue valueWithCGPoint:CGPointMake(320,480)];
    traslation.duration = 2.0;
    //traslation.autoreverses = YES;
    traslation.repeatCount = 1;

    [self.m_image.layer addAnimation:traslation forKey:@"traslation"];
}

//透明
- (IBAction)opacity:(id)sender {
    CABasicAnimation *opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacity.fromValue = [NSNumber numberWithFloat:1.0];
    opacity.toValue = [NSNumber numberWithFloat:0.4];
    opacity.duration = 0.2; //动画时间
    opacity.repeatCount = FLT_MAX; //永久
    opacity.autoreverses = YES; //每次动画后倒回回放
    opacity.removedOnCompletion=NO;  //动画后不还原,为no时不回到最初状态
    opacity.fillMode=kCAFillModeForwards;
    
    [self.m_image.layer addAnimation:opacity forKey:@"opacity"];
}

// 旋转
- (IBAction)rotate:(id)sender {
    CATransform3D ca3d = CATransform3DMakeRotation(45 * 3.14159265/180.0, -1, 1, 1);
    
    CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.toValue = [NSValue valueWithCATransform3D:ca3d];
    rotate.duration=1.0;
    rotate.autoreverses=NO;
    rotate.repeatCount=1;
    rotate.removedOnCompletion=NO;
    rotate.fillMode=kCAFillModeForwards;
    [self.m_image.layer addAnimation:rotate forKey:@"rotate"];
}

- (IBAction)alpha:(id)sender {
}

//缩放
- (IBAction)scale:(id)sender {
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue=[NSNumber numberWithFloat:0.5];
    scale.toValue = [NSNumber numberWithFloat:2.0];
    scale.duration=1.0;
    scale.autoreverses=YES;
    scale.repeatCount=2;
    scale.removedOnCompletion=YES;
    scale.fillMode=kCAFillModeForwards;
    [self.m_image.layer addAnimation:scale forKey:@"scale"];
}

//不按原始边长度缩放
-(IBAction)bounds:(id)sender{
    CABasicAnimation *bounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
    bounds.duration = 1.f;
    bounds.fromValue = [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];
    bounds.toValue = [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];
    bounds.byValue  = [NSValue valueWithCGRect:self. m_image.bounds];

    bounds.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    bounds.repeatCount = 1;
    bounds.autoreverses = YES;
    
    [self.m_image.layer addAnimation:bounds forKey:@"bounds"];
}

- (IBAction)path:(id)sender {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, 0);
    //添加直线路径
    CGPathAddLineToPoint(path, NULL, 60, 130);
    CGPathAddLineToPoint(path, NULL, 70, 140);
    CGPathAddLineToPoint(path, NULL, 80, 150);
    CGPathAddLineToPoint(path, NULL, 90, 160);
    CGPathAddLineToPoint(path, NULL, 100, 170);
    //添加曲线路径
    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;
    animation.duration = 5;
    animation.autoreverses = YES;
    [self.m_image.layer addAnimation:animation forKey:@"path"];
    CFRelease(path);
}
//组合动画
- (IBAction)goup:(id)sender {
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue=[NSNumber numberWithFloat:0.5];
    scale.toValue = [NSNumber numberWithFloat:2.0];
    
    CABasicAnimation *traslation = [CABasicAnimation animationWithKeyPath:@"position"];
    traslation.toValue = [NSValue valueWithCGPoint:CGPointMake(320,480)];
    
    group.animations=[NSArray arrayWithObjects:scale, traslation, nil];
    group.duration = 2.0;
    
    [self.m_image.layer addAnimation:group forKey:@"group"];

}





其中CABasicAnimation代理函数:

动画开始

- (void)animationDidStart:(CAAnimation *)anim;

动画结束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;


有个动画小demo  启动页

http://download.csdn.net/detail/u010486174/6586601

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值