CoreAnimation 是一个抽象类,是所有动画的底层实现。系统提供了以下几个子类:
CABasicAnimation 基础动画 ,对属性的变化作动画,只做一次动画;
CAKeyframeAnimation CAAnimationGroup CATransitoin 转场动画页面切换效果动画
CASpringAnimation 弹性动画 抖动效果
achorPoint 视图上的某一点,默认为0.5 0.5 大小为0-1之间
position 以父视图0 0为原点,确定的achorpoint的位置
注意:CoreAnimation并不会对自身创建的视图作动画,而是重新创建一个一模一样的layer专门来作动画,触摸时间还是在原来的位置上生效。
iOS系统中的动画有隐式动画、可式动画两种:
每个手动创建的CALayer都存在个隐式动画,隐式动画可以关闭setDisableAction=yes
对根图层进行属性修改时,产生的动画叫做可式动画
1 #pragma mark - 关键帧动画 2 -(void)keyFrameAnimation 3 { 4 //创建动画对象 5 CAKeyframeAnimation *keyAnim =[CAKeyframeAnimation animation]; 6 //设置需要动画的属性 7 keyAnim.keyPath = @"position"; 8 //设置动画属性 9 keyAnim.values = @[[NSValue valueWithCGPoint:CGPointZero],[NSValue valueWithCGPoint:CGPointMake(50, 100)],[NSValue valueWithCGPoint:CGPointMake(100, 50)],[NSValue valueWithCGPoint:CGPointMake(100, 100)]];10 keyAnim.duration = 3;11 12 //默认每一帧的动画时间是相等的,可以手动设置,参数为0-1的比例13 keyAnim.keyTimes = @[@(0),@(0.7),@(0.8),@(1)];14 15 //设置动画结束后视图的位子不变16 keyAnim.removedOnCompletion = NO;17 keyAnim.fillMode = kCAFillModeForwards;18 19 //创建贝赛尔曲线,按路径动画。20 //keyAnim.path =21 22 //为视图添加动画23 [self.button.layer addAnimation:keyAnim forKey:@"这是动画标识"];24 25 }
1 #pragma mark - 动画组 2 -(void)animationGroup 3 { 4 //创建动画a1 5 CABasicAnimation *a1 = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 6 7 a1.fromValue = (id)[UIColor redColor].CGColor; 8 a1.toValue = (id)[UIColor blueColor].CGColor; 9 10 //这里的transform的属性有旋转、缩放、平移,详情见表。11 CABasicAnimation *a2 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];12 13 a2.fromValue = @(0);14 a2.toValue = @(50);15 16 //CATransform3D有旋转、缩放、平移17 CABasicAnimation *a3 = [CABasicAnimation animationWithKeyPath:@"transform"];18 19 a3.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];20 a3.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 0.5, 0.5)];21 22 CAAnimationGroup *aniGroup = [CAAnimationGroup animation];23 24 aniGroup.animations = @[a1, a2,a3];25 aniGroup.duration = 3.0f;26 27 aniGroup.removedOnCompletion = NO;28 aniGroup.fillMode = kCAFillModeForwards;29 30 [self.button.layer addAnimation:aniGroup forKey:@"d"];31 }
1 #pragma mark -转场动画CATransition 2 -(void)transition 3 { 4 NSLog(@"一般在两个控制器间实现"); 5 6 //获取转场动画 7 CATransition *transitionAni = [CATransition animation]; 8 9 //系统私有的转场动画:cube、suckEffect,oglFlip,rippleEffect,pageCurl,pageUncurl10 //cameraIrishollowOpen,cameraIrishollowClose,使用这些动画上架不会通过。11 12 //设置动画方式13 14 transitionAni.type = @"moveIn";15 //设置动画方向16 transitionAni.subtype = kCATransitionFromRight;17 //动画时间18 transitionAni.duration = 1;19 20 //添加动画21 [self.button.layer addAnimation:transitionAni forKey:@"transition"];22 23 }
1 #pragma mark -弹性动画 2 -(void)springAnimation 3 { 4 CASpringAnimation *springAni = [CASpringAnimation animationWithKeyPath:@"position"]; 5 6 springAni.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 200)]; 7 springAni.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 300)]; 8 9 //设置弹性动画的特有参数,可选10 11 //质量12 springAni.mass = 1;13 14 //刚性系数15 springAni.stiffness = 50;16 17 //阻尼系数18 springAni.damping = 5;19 20 //初始速度21 springAni.initialVelocity = 5;22 23 //估算动画时间,必须设置24 springAni.duration = springAni.settlingDuration;25 26 springAni.removedOnCompletion = NO;27 springAni.fillMode = kCAFillModeForwards;28 29 [self.button.layer addAnimation:springAni forKey:@"c"];30 }
1 #pragma mark - 封装过的核心动画 2 -(void)UIAnimation 3 { 4 CGAffineTransform transform3D; 5 6 transform3D = CGAffineTransformMakeScale(0.2, 0.5); 7 8 [UIView beginAnimations:@"标示" context:nil]; 9 10 [UIView setAnimationDuration:2 ];11 12 [UIView setAnimationDelegate:self];13 14 [self.button setTransform:transform3D];15 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.button cache:NO];16 17 [UIView commitAnimations];18 }