话不多说,直接上代码,里面该有的注释都有了,一看就懂,多敲几遍就行了
// 平移动画
- (void)translationAnimation{
UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 50, 50)];
[self.view addSubview:kLView];
kLView.layer.borderColor = [UIColor yellowColor].CGColor;
kLView.layer.borderWidth = 2;
kLView.backgroundColor = [UIColor redColor];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
animation.duration = 2;
CGFloat width = self.view.frame.size.width;
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(width - 50, 0)];
// 指定动画重复多少圈是累加的
animation.cumulative = 2;
// 动画完成后不自动删除
animation.removedOnCompletion = NO;
// 设置移动的效果为快如快出的效果
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 设置无限循环动画
animation.repeatCount = HUGE_VALF;
// 设置动画完成后,自动以动画的回到原点
animation.autoreverses = YES;
// 设置东环完成后,返回到原点
animation.fillMode = kCAFillModeBackwards;
[kLView.layer addAnimation:animation forKey:@"transform.translation"];
}
// 旋转动画
- (void)rotationAnimation{
UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 240, 50, 50)];
[self.view addSubview:kLView];
kLView.layer.borderColor = [UIColor yellowColor].CGColor;
kLView.layer.borderWidth = 2;
kLView.backgroundColor = [UIColor redColor];
// 这里使用的KVC的思想
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.01;
// z轴旋转180度
CATransform3D transform3d = CATransform3DMakeRotation(3.1415, 0, 0, 180);
animation.toValue = [NSValue valueWithCATransform3D:transform3d];
animation.cumulative = YES;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = HUGE_VALF;
animation.autoreverses = YES;
animation.fillMode = kCAFillModeBackwards;
[kLView.layer addAnimation:animation forKey:@"transform"];
}
// 缩放动画
- (void)scaleAnimation{
UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, 50, 50)];
[self.view addSubview:kLView];
kLView.layer.borderColor = [UIColor yellowColor].CGColor;
kLView.layer.borderWidth = 2;
kLView.backgroundColor = [UIColor redColor];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 2;
animation.fromValue = @(1);
animation.toValue = @(0);
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = HUGE_VALF;
animation.autoreverses = YES;
animation.fillMode = kCAFillModeBackwards;
[kLView.layer addAnimation:animation forKey:@"transform.scale"];
}
// 闪烁动画
- (void)flashAnimation
{
UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 50, 50)];
[self.view addSubview:kLView];
kLView.layer.borderColor = [UIColor yellowColor].CGColor;
kLView.layer.borderWidth = 2;
kLView.backgroundColor = [UIColor redColor];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration = 0.001;
animation.fromValue = @(1);
animation.toValue = @(0);
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = HUGE_VALF;
animation.autoreverses = YES;
animation.fillMode = kCAFillModeBackwards;
[kLView.layer addAnimation:animation forKey:@"opacity"];
}
// 路径动画
- (void)routeAnimation{
UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
kLView.layer.borderWidth = 2;
kLView.layer.borderColor = [UIColor yellowColor].CGColor;
kLView.backgroundColor = [UIColor redColor];
[self.view addSubview:kLView];
// 添加动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
/**
*
Both fromValue and toValue are non-nil. Interpolates between fromValue and toValue.
fromValue and byValue are non-nil. Interpolates between fromValue and (fromValue + byValue).
byValue and toValue are non-nil. Interpolates between (toValue - byValue) and toValue.
看到这里是不是有点晕,莫怕,带我慢慢说来 ,假设我们都是用fromValue和toValue来进行处理,
当程序中使用的是fromValue和toValue时,那么fromValue = fromValue,
toValue = toValue,
如果程序中是fromValue和byValue,那么fromValue = fromValue ,toValue = (fromValue + byValue);
如果程序中是toValue和byValue,那么fromValue = (toValue - byValue),toValue = toValue;
*/
// 起点 这个值是指position,也就是layer的中心值
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
// 终点 这个值是指position,也就是layer的中心值
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 50, self.view.bounds.size.width - 100)];
animation.byValue = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 50, self.view.bounds.size.width - 100)];
// 线性动画
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.removedOnCompletion = NO;
animation.duration = 2;
// 这里是播放速率 1表示正常播放,小于1则是慢速,大于1则是快速
animation.speed = 0.5;
animation.beginTime = 0;
animation.repeatCount = HUGE_VALF;
animation.autoreverses = YES;
animation.fillMode = kCAFillModeBackwards;
[kLView.layer addAnimation:animation forKey:@"position"];
}