首先创建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 [email protected]"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];
}