CAAnimation、CAPropertyAnimation类不能直接使用。一般使用子类CABasicAnimation、CAKeyframeAnimation、CASpringAnimation、CATransition、CAAnimationGroup以及CATransaction。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
self.animationView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
self.animationView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:self.animationView];
[UIView setAnimationsEnabled:YES];
}
CABasicAnimation
CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
ba.toValue = (__bridge id _Nullable)([UIColor redColor].CGColor);
ba.byValue = (__bridge id _Nullable)([UIColor purpleColor].CGColor);
ba.duration = 3.0;
// forKey:可以自定义
[self.animationView.layer addAnimation:ba forKey:kCATransition];
CAKeyframeAnimation
CAKeyframeAnimation *kfa = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
CAKeyframeAnimation *kfa2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
kfa.values = @[(__bridge id _Nullable)([UIColor redColor].CGColor), (__bridge id _Nullable)([UIColor purpleColor].CGColor)];
kfa.duration = 3.0;
// calculationMode值为kCAAnimationLinear or kCAAnimationCubic,此值必须为@[@0.0, ...... @1.0]
// calculationMode值为kCAAnimationDiscrete,此值必须为@[@0.0, ...... @1.0],该数组 count >= 3
// calculationMode值为kCAAnimationPaced or kCAAnimationCubicPaced,此值忽略
kfa.keyTimes = @[@0.3, @0.5];
kfa.calculationMode = kCAAnimationCubicPaced;
kfa.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
// create a CGPath that implements two arcs (a bunce)
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.animationView.center.x, self.animationView.center.y);
CGPathAddCurveToPoint(path, NULL, 74.0, 500.0, 320.0, 500.0, 320.0, 74.0);
// CGPathAddCurveToPoint(path, NULL, 320.0, 500.0, 566.0, 500.0, 566.0, 74.0);
kfa2.path = path;
kfa2.duration = 5.0;
kfa2.rotationMode = kCAAnimationRotateAuto;
kfa2.tensionValues = @[@(-1), @(-0.5), @0.5, @1];
kfa2.continuityValues = @[@(-1), @(-0.5), @0.5, @1];
kfa2.biasValues = @[@(-1), @(-0.5), @0.5, @1];
[self.animationView.layer addAnimation:kfa forKey:@"backfroundColor"];
[self.animationView.layer addAnimation:kfa2 forKey:@"position"];
CASpringAnimation
CASpringAnimation *sa = [CASpringAnimation animationWithKeyPath:@"position.y"];
sa.initialVelocity = -1;
sa.fromValue = @100;
sa.toValue = @500;
sa.damping = 0.7;
sa.stiffness = 120;
sa.duration = 8.0;
sa.mass = 5;
[self.animationView.layer addAnimation:sa forKey:@"position.y"];
CATransition
- (void)transitionsAnimation{
CATransition *transition = [[CATransition alloc] init];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.duration = 3;
transition.startProgress = 0.2;
transition.endProgress = 0.9;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
[self.animationView.layer addAnimation:transition forKey:@"transition"];
}
CAAnimationGroup
- (void)basicAnimationValueFunction{
// transform.scale transform.translation 对单一方向设置不需配置valueFunction值
CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"transform"];
ba.fromValue = @[@1, @1, @1];
ba.toValue = @[@2, @5, @2];
// CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"transform"];
// ba.fromValue = @[@0, @0, @0];
// ba.toValue = @[@200, @400, @50];
ba.duration = 3.0;
ba.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
ba.removedOnCompletion = NO;
ba.fillMode = kCAFillModeForwards;
ba.autoreverses = YES;
CAValueFunction *vf = [CAValueFunction functionWithName:kCAValueFunctionScale];
ba.valueFunction = vf;
[self.animationView.layer addAnimation:ba forKey:kCATransition];
}
[self basicAnimationValueFunction];
[self transitionsAnimation];
[self.animationView.layer addAnimation:self.animationGroup forKey:@"animationGroup"];
CATransaction
[CATransaction begin];
[CATransaction setDisableActions:NO];
[CATransaction setAnimationDuration:5.0];
[CATransaction setCompletionBlock:^{
NSLog(@"completion");
}];
CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithControlPoints:0.1 :0.9 :0.9 :0.1];
[CATransaction setAnimationTimingFunction:tf];
// [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
// [CATransaction setValue:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut] forKey:kCATransactionAnimationTimingFunction];
// UIView 控件不能直接操作 .layer 属性, 而要用 [UIView animate.....
[UIView animateWithDuration:5.0 animations:^{
self.animationView.center = CGPointMake(100, 400);
self.animationView.alpha = 0.5;
self.animationView.layer.cornerRadius = 40;
}];
[CATransaction begin];
[CATransaction commit];
[CATransaction commit];