首先创建layer
CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(100, 100);
layer.backgroundColor = [UIColor yellowColor].CGColor;
[self.view.layer addSublayer:layer];
self.layer = layer;
设置点击事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// [self animationScale];
[self keyAnimation];
}
缩小动画
-(void)animationScale{
CABasicAnimation *anim = [CABasicAnimation animation];
//2设置动画
// anim.keyPath = @"bounds";//平移
// anim.keyPath = @"position";//缩放
anim.keyPath =@"transform";
//到达哪个点
// anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];
// anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300,300)];
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];
anim.duration = 2;
anim.removedOnCompletion = NO;
anim.fillMode = @"forwards";
[self.layer addAnimation:anim forKey:nil];
}
-(void)keyAnimation
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.layer addAnimation:anim forKey:nil];
}
四种动画
基础动画
关键帧动画
转场动画
动画组合
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animation];
keyFrame.duration = 2.0;
keyFrame.keyPath = @"position";
keyFrame.removedOnCompletion = NO;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, CGRectMake(30, 50, 300, 300));
keyFrame.path = path;
[self.imgView.layer addAnimation:keyFrame forKey:nil];
CATransition *transition = [CATransition animation];
transition.type = @"cube";
[self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"];
AnotherViewController *anotherVC =[[AnotherViewController alloc]init];
[self.navigationController showViewController:anotherVC sender:nil];
- (IBAction)exchangebtn {
// CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animation];
// keyFrame.duration = 2.0;
// keyFrame.keyPath = @"position";
// keyFrame.removedOnCompletion = NO;
// CGMutablePathRef path = CGPathCreateMutable();
// CGPathAddEllipseInRect(path, nil, CGRectMake(30, 50, 300, 300));
// keyFrame.path = path;
CABasicAnimation *basic = [CABasicAnimation animation];
basic.keyPath = @"bounds";
basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];
basic.duration = 2.0;
basic.removedOnCompletion = YES;
CABasicAnimation *basic1 = [CABasicAnimation animation];
basic1.duration = 2.0;
basic1.keyPath = @"position";
basic1.toValue = [NSValue valueWithCGPoint:CGPointMake(100,500)];
basic1.fillMode = @"forwards";
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[basic,basic1];
[self.imgView.layer addAnimation:group forKey:nil];
// CATransition *trasition = [CATransition animation];
// trasition.type = @"pageCurl";
// trasition.subtype = kCATransitionFade;
// [self.imgView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// [self.imgView.layer addAnimation:trasition forKey:@"myAnimation"];
// CATransition *transition = [CATransition animation];
// transition.duration = 1;
//// transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
// transition.type = @"pageCurl";
//
// transition.subtype = kCATransitionFromRight;
// [self.imgView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// [self.imgView.layer addAnimation:transition forKey:@"myAnimation"];
}
- (IBAction)push {
CATransition *transition = [CATransition animation];
transition.type = @"cube";
[self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"];
AnotherViewController *anotherVC =[[AnotherViewController alloc]init];
[self.navigationController showViewController:anotherVC sender:nil];
// CATransition *transition = [CATransition animation];
// transition.type = @"cube";
//// transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
// [self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"];
//
// AnotherViewController *anotherVC = [[AnotherViewController alloc]init];
// [self.navigationController showViewController:anotherVC sender:nil];
}