核心动画就是CoreAnimation直译过来的中文,它是一组非常强大的动画处理API,只需要使用少量代码就能实现炫酷的动画效果。
可以使用的核心动画只有四种: - CABasicAnimation - CAKeyframeAnimation - CATransition - CAAnimationGroup 其中CABasicAnimation和CAKeyframeAnimation是CAPropertyAnimation的子类,他是一个抽象类,想要创建动画对象,应该使用它的子类。
/**CABasicAnimation的position演示*/ -(void)CABasicAnimationSetupPosition { //1.创建基础核心动画 CABasicAnimation *basic = [CABasicAnimation animation]; //设置要执行什么样的动画 basic.keyPath = @"position"; //动画起始位置,若不设置就默认当前为止开始 basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; //动画结束为止 basic.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; //动画执行时间 basic.duration = 3.0; /* 以下两句是强行改变动画为止和强行保留动画结束状态 默认是结束后移除动画,并且回到原始为止 */ //设置动画执行完毕后不删除动画 basic.removedOnCompletion = NO; //设置动画结束后保留最后的状态 basic.fillMode = kCAFillModeForwards; //2.添加核心动画到layer [self.layer addAnimation:basic forKey:nil]; }
/**CAKeyframeAnimation的postion演示1*/ -(void)CAKeyframeAnimationSetupPosition1 { //矩形运动轨迹 CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"]; keyframe.delegate = self; NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(300, 100)]; NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(100, 300)]; NSValue *v5 = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; keyframe.fillMode = kCAFillModeForwards; keyframe.removedOnCompletion = NO; //动画效果 keyframe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; keyframe.duration = 2.0; keyframe.values = @[v1,v2,v3,v4,v5]; [self.layer addAnimation:keyframe forKey:nil]; }
/**CAKeyframeAnimation的rotation演示(抖动效果)*/ -(void)CAKeyframeAnimationShakeEffect { CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"]; keyframe.removedOnCompletion = NO; keyframe.fillMode = kCAFillModeForwards; keyframe.duration = 0.1; keyframe.values = @[@(-angle(4)),@(angle(4)),@(-angle(4))]; keyframe.repeatCount = MAXFLOAT; //而且直接添加到self.view.layer的keyframe 和直接添加到self.imageView.layer的keyframe效果不一样 //前者以左上角旋转,后者以中心点旋转 [self.imageView.layer addAnimation:keyframe forKey:nil]; }
UIView动画变化后,他的真实位置是变化了(center,或者frame)。但是,核心动画变化之后,他的真实位置并没有变化。看到的都是假象而已(实际上是他的layer没有变化) `/*UIView动画和核心动画的一些区别/ -(void)differentBetweenUIviewAndCALayer { //这里的self.imageView.center变化了 [UIView animateWithDuration:2.0 animations:^{ NSLog(@”doAnimation before: %@”,NSStringFromCGPoint(self.imageView.center)); self.imageView.center = CGPointMake(300, 300); [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; } completion:^(BOOL finished) { NSLog(@”doAnimation after: %@”,NSStringFromCGPoint(self.imageView.center)); }]; //可以发现,self.imageView2.center根本就没有变化 NSLog(@”doCoreAnimation before: %@”,NSStringFromCGPoint(self.imageView2.center)); [self CAAnimationGroupShow]; NSLog(@”doCoreAnimation after: %@”,NSStringFromCGPoint(self.imageView2.center));
}`
核心动画所展示的都是假象,并不会真是修改layer的属性(比如它的frame),我们只是强制保留动画结束的状态,并且不移除它而已。 核心动画使用场景:最多的是用转场动画、加入购物车功能也会用到组动画 不需要与用户交互就用核心动画,需要和用户交互就用UIView
https://github.com/HZhenF/CoreAnimation.git 参考代码