关于 Core Animation
Core Animation是一组非常强大的动画处理API,使用它能做出很多优雅的动画效果。能用的动画类有4个子类:CABasicAnimation、CAKeyframeAnimation、CATransition、CAAnimationGroup
开发步骤:
**
- 初始化一个动画对象(CAAnimation)并设置一些动画相关属性.
- 添加动画对象到层(CALayer)中,开始执行动画.
CALayer中很多属性都可以通过CAAnimation实现动画效果, 包括opacity, position, transform, bounds, contents等,具体可以在API文档中查找
通过调用CALayer的addAnimation:forKey:增加动画到层(CALayer)中,这样就能触发动画了.通过调用removeAnimationForKey:可以停止层中的动画.
注:Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程.
巧妙的运用这些可以属性实现很棒的动画效果,比如下面:用CABasicAnimation实现的动画
CABasicAnimation动画
简单的呼吸和摇摆动画
简单的代码
1.呼吸动画
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];
animation.autoreverses = YES; //回退动画(动画可逆,即循环)
animation.duration = 1.0f;
animation.repeatCount = MAXFLOAT;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;//removedOnCompletion,fillMode配合使用保持动画完成效果
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.alphaTagButton.layer addAnimation:animation forKey:@"aAlpha"];
2.摇摆动画
//设置旋转原点
self.sharkTagButton.layer.anchorPoint = CGPointMake(0.5, 0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//角度转弧度(这里用1,-1简单处理一下)
rotationAnimation.toValue = [NSNumber numberWithFloat:1];
rotationAnimation.fromValue = [NSNumber numberWithFloat:-1];
rotationAnimation.duration = 1.0f;
rotationAnimation.repeatCount = MAXFLOAT;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.autoreverses = YES;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.fillMode = kCAFillModeForwards;
[self.sharkTagButton.layer addAnimation:rotationAnimation forKey:@"revItUpAnimation"];
CATransition之简单的转场动画
CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。
常用动画类型:
注:私有API只能通过字符串使用
过渡方向参数:
简单的CATransition动画
-(void)animationWithType:(NSString*)type
{
//- 创建一个转场动画:
CATransition *transition = [CATransition animation];
transition.repeatCount = 5;
// - 确定动画类型:
transition.type = type;
// - 确定子类型(方向等)
transition.subtype = kCATransitionFromLeft;
// - 确定动画时间
transition.duration = 1;
// - 添加动画
[self.imageView.layer addAnimation:transition forKey:nil];
}
使用时只用传你的想要的动画类型就好,私有API只能通过字符串使用哈。
[self animationWithType:self.dataArray[indexPath.row]];