CAAnimation 动画

//----------------------- 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"];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值