CABasicAnimation用法和一些簡單的動畫效果,移動,旋轉,縮放


CABasicAnimation 自己只有三个property   fromValue  toValue  ByValue

当你创建一个 CABasicAnimation 时,你需要通过-setFromValue 和-setToValue 来指定一个开始值和结束值。 当你增加基础动画到层中的时候,它开始运行。当用属性做动画完成时,例如用位置属性做动画,层就会立刻 返回到它的初始位置 

记住当你做动画时,你至少使用了 2 个对象。这些对象都是层本身,一个层或者层继承的对象,和在先前 的例子中你分配给层的 CABasicAnimation 对象。因为你给动画对象设定了最后的值(目的地),但是并不意 味着当动画完成的时候,层的属性就改变成了最后的值。当动画完成时,你必须显示的设定层的属性,这样动 画结束后,你的层才能真正的到你设定的属性值上。

你可以简单的停止动画到你结束的点上,但是这仅仅是一个视觉效果。层实际的值仍然是一样的。要真的 改变内部的值,就像刚才所说的你必须显示的设定那个属性。例如,显示的设定位置的属性,你需要在层中调 用-setPosition 方法。但是,这会造成一点问题。

如果你通过-set 这个方法显示的设定了层属性的值,那么默认的动画将被执行,而非之前你设定的动画。 在表 3-9 中演示了你设置位置的方法。注意到了,我们使用 position 已经创建了基础动画,但是我们在层上显 示的调用了-setPosition 方法,就覆盖了我们设定的动画,使我们设定的基础动画完全没用了。如果你使用了这 个代码,你会看到虽然我们的层结束的时候放到了正确的位置,但是它使用的是默认的 0.25 秒,而非我们在 动画里显示设定的 5 秒钟。 

CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@”position”]; 
[animation setFromValue:[NSValue valueWithPoint:startPoint]]; 
[animation setToValue:[NSValue valueWithPoint:endPoint]];                                                                              //設置動畫時間
[animation setDuration:5.0];                                                 
[layer setPosition:endpoint];
[layer addAnimation:animation forKey:nil];

因此现在问题出来了,你怎么能使用我们设定的动画呢?看表 3-9 的最后一行,注意到 forKey:这个参数 是被设定为 nil。这就是为什么动画不能覆盖默认动画的原因。如果你改变最后一行为[layer addAnimation:animation forKey:@"position"],动画将会按照我们设定的时间工作。这告诉了层当需要做动画时, 使用我们给关键路径指定的新动画。

左右移動效果:

- (void)loadView { 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide]; 
    UIImage *image=[UIImage imageNamed:@"1.jpg"]; 
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, 768, 1024, 8, 4 * 768, colorSpace, kCGImageAlphaPremultipliedFirst); 
    CGRect rect = CGRectMake(0, 0, 768, 1024); 
    CGColorRef fillColor = [[UIColor whiteColor] CGColor]; 
    CGContextSetFillColor(context, CGColorGetComponents(fillColor)); 
    
    CGContextMoveToPoint(context, 160.0f, 230.0f); 
    CGContextAddLineToPoint(context, 600.0f, 230.0f); 
    CGContextAddLineToPoint(context, 600.0f, 100.0f); 
    CGContextAddLineToPoint(context, 370.0f, 50.0f); 
    CGContextAddLineToPoint(context, 200.0f, 100.0f); 
    
    CGContextClosePath(context); 
    CGContextClip(context); 
    CGContextDrawImage(context, rect, image.CGImage); 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked]; 
    CGImageRelease(imageMasked); 
    
    UIImageView *backView=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    [self.view addSubview:backView]; 
    backView.image=newImage; 
    backView.alpha=0.3; 
    
    CABasicAnimation *theAnimation1;    //定义动画 
    
    //左右摇摆 
    theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 
    theAnimation1.fromValue=[NSNumber numberWithFloat:0]; 
    theAnimation1.toValue=[NSNumber numberWithFloat:-100]; 
    
    theAnimation1.duration=5.5;//动画持续时间 
    theAnimation1.repeatCount=6;//动画重复次数 
    theAnimation1.autoreverses=YES;//是否自动重复 
    
    [backView.layer addAnimation:theAnimation1 forKey:@"animateLayer"]; 
    
    [newImage release]; 
    [image release]; 
}

旋轉效果:

  theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform"]; 
    theAnimation1.toValue = [ NSValue valueWithCATransform3D: CATransform3DMakeRotation(3.1415, 0, 0, 1.0) ];

縮放

  theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform"]; 
    theAnimation1.toValue = [ NSValue valueWithCATransform3D: CATransform3DMakeRotation(3.1415, 0, 0, 1.0) ];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值