Core Animation - 属性动画CAPropertyAnimation

来源:互联网 发布:手机测音软件 编辑:程序博客网 时间:2024/05/21 14:04

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就可以了。

0 0