//----------------------- CATransition ----------------
//转场动画
- (void)CATransitionAction
{
// 实现:图片浏览
/** 转场代码 */
static int index = 2;
NSString *imageName = [NSString stringWithFormat:@"%d",index];
_imgView.image = [UIImage imageNamed:imageName];
index++;
if (index == 4) {
index = 1;
}
/** 转场动画代码 */
// 创建转场动画对象
CATransition *anim = [CATransition animation];
// 设置转场类型
anim.type = @"cube";//pageCurl - 翻页, cube - 立方体翻转
// 设置动画的方向
anim.subtype = kCATransitionFromLeft;
anim.duration = 3;
[_imgView.layer addAnimation:anim forKey:nil];
}
//------------------ CABasicAnimation ------------------
//移动
- (void)move:(UIView*)view
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 2;
animation.repeatCount = 2;
animation.beginTime = CACurrentMediaTime() + 3;// 3秒后执行
// animation.autoreverses = YES;
animation.fromValue = [NSValue valueWithCGPoint:self.view.layer.position]; // 起始帧
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; // 终了帧
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
// 视图添加动画
[view.layer addAnimation:animation forKey:@"move-layer"];
}
//旋转
- (void)rotation:(UIView*)view
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 2;
animation.repeatCount = 2;
animation.beginTime = CACurrentMediaTime() + 1; // 1秒后执行
animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
animation.toValue = [NSNumber numberWithFloat:M_PI/4.0]; // 终止角度
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[view.layer addAnimation:animation forKey:@"rotate-layer"];
}
//缩放
- (void)scale:(UIView*)view
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 2.5; // 动画持续时间
animation.repeatCount = 1; // 重复次数
animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
animation.toValue = [NSNumber numberWithFloat:2.0]; // 结束时的倍率
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[view.layer addAnimation:animation forKey:@"scale-layer"];
}
//渐隐
- (void)opacity:(UIView*)view
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = @(1.0);
animation.toValue = @(0.2);
animation.duration = 2;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[view.layer addAnimation:animation forKey:@"opacity-layer"];
}
//组合动画
- (void)group:(UIView*)view
{
CABasicAnimation *animation1 =[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animation1.toValue = [NSNumber numberWithFloat:80];; // 終点
CABasicAnimation *animation2 =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation2.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的角度
animation2.toValue = [NSNumber numberWithFloat:M_PI/2.0]; // 结束时的角度
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 1.0;
group.repeatCount = 1;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.animations = [NSArray arrayWithObjects:animation1, animation2, nil];
[view.layer addAnimation:group forKey:@"move-rotate-layer"];
}
//------------------- CAKeyframeAnimation ---------------------
- (void)CAKeyframeAnimationAction:(UIView*)view
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 100);
CGPathAddQuadCurveToPoint(path, NULL, 150, 200, 300, 100);
animation.path = path;//还有 设置 values 方案
CGPathRelease(path);
animation.duration = 2;
[view.layer addAnimation:animation forKey:@"CAKeyframeAnimation-position"];
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 2;
rotationAnimation.cumulative = YES;
[view.layer addAnimation:rotationAnimation forKey:@"aa"];
}
CAAnimation 动画
最新推荐文章于 2018-12-23 01:10:00 发布