Core Animation - 属性动画CAPropertyAnimation

core animation里面有个很重要的类CAPropertyAnimation,它有两个子类,CABasicAnimation和CAKeyFrameAnimation。

类图如下:


CABasicAnimation可以设置一些属性,比如位置从A到B,然后就会移动过去。

CAKeyFrameAnimation基本就和flash里面说的补间动画一样(tweening),设置一些值,比如A,B,C,D4个位置,然后A和B之间,会根据一定的算法不一些位置。

应该说CAKeyFrameAnimation会比CABasicAnimation高级智能一些,动画看起来也会更加平滑一点。

具体使用都是很简单,下面有个例子,创建了关键帧动画和基本动画:

    CALayer *sublayer =[CALayer layer];
    
    sublayer.backgroundColor =[UIColor orangeColor].CGColor;
    sublayer.shadowOffset = CGSizeMake(0, 3);
    sublayer.shadowRadius =5.0;
    sublayer.shadowColor =[UIColor blackColor].CGColor;
    sublayer.shadowOpacity = 1;
    
    sublayer.borderColor =[UIColor blackColor].CGColor;
    sublayer.borderWidth =2.0;
    sublayer.cornerRadius =10.0;
    sublayer.anchorPoint = CGPointMake(0, 0);
    [self.view.layer addSublayer:sublayer];

    CGImageRef img = [UIImage imageNamed:@"Image"].CGImage;
    sublayer.contents = (__bridge id)img;
    sublayer.frame = CGRectMake(180, 60, CGImageGetWidth(img), CGImageGetWidth(img));
    
    
    CGPoint fromPoint = sublayer.frame.origin;
    
    //路径曲线
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:fromPoint];
    CGPoint toPoint = CGPointMake(30, 360);
    [movePath addQuadCurveToPoint:toPoint
                     controlPoint:CGPointMake(300,0)];

    
    //关键帧
    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath; 
    moveAnim.removedOnCompletion = YES;
    moveAnim.duration = 3;
    moveAnim.delegate = self;
    
    //旋转变化
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //x,y轴缩小到0.1,Z 轴不变
    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
    scaleAnim.removedOnCompletion = YES;
    scaleAnim.duration = 5;
    scaleAnim.delegate = self;
    
    //透明度变化
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
    opacityAnim.removedOnCompletion = YES;
    opacityAnim.delegate = self;

上面的代码里面有一个关键帧动画和两个基本动画,直接把它们add到CALayer就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值