各种动画效果
1.给定圆心,围绕圆心来转动
UIView *anmicationView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 4, 4)];
anmicationView.backgroundColor = [UIColor redColor];
[self.view addSubview:anmicationView];
CAKeyframeAnimation *orbitAnimation = [CAKeyframeAnimation animation];
orbitAnimation.keyPath = @"position";
//创建一个圆形的 CGPath 作为我们的关键帧动画的 path
orbitAnimation.path = CFAutorelease(CGPathCreateWithEllipseInRect(CGRectMake(20, 100, 72, 72), NULL));
orbitAnimation.duration = .9;
orbitAnimation.additive = YES;
orbitAnimation.repeatCount = HUGE_VALF;
//使用 calculationMode 是控制关键帧动画时间的另一种方法。我们通过将其设置为 kCAAnimationPaced,让 Core Animation 向被驱动的对象施加一个恒定速度,不管路径的各个线段有多长。将其设置为 kCAAnimationPaced 将无视所有我们已经设置的 keyTimes。
orbitAnimation.calculationMode = kCAAnimationPaced;
//确保view沿着路径旋转
orbitAnimation.rotationMode = kCAAnimationRotateAuto;
[anmicationView.layer addAnimation:orbitAnimation forKey:@"test"];
2.自己沿着自己的圆心自传
UIView *animationView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 10, 10)];
[self.view addSubview:animationView];
CABasicAnimation* rotationAnimation;
animationView.backgroundColor = [UIColor redColor];
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VALF;
[animationView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
3.沿着特定的点,联系来的路径来执行动画效果
UIView *anmicationViews = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 4, 4)];
anmicationViews.backgroundColor = [UIColor redColor];
[self.view addSubview:anmicationViews];
CGMutablePathRef path = CGPathCreateMutable(); //初始创建路线
CGPathMoveToPoint(path, NULL, 40, 80); //路线起点
CGPathAddLineToPoint(path, NULL,80 , 150); //连线到第二个点
CGPathAddLineToPoint(path, NULL,100 , 90); //连线到第三个点
CGPathAddLineToPoint(path, NULL, 200, 200);
CGPathCloseSubpath(path); //路线close起来,即让第三个点和第一个点自动连接
CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path = path; //动画路线
theAnimation.duration = .5; //动画总时间
theAnimation.repeatCount = HUGE_VALF;//时间的最大值
CFRelease(path);
[anmicationViews.layer addAnimation:theAnimation forKey:@"position"]; //让动画的执行者开始执行动画