ios-核心动画CoreAnimation

核心动画其实就是Core Animation可以用在Mac OS X 和iOS的平台

Core Animation 的动画执行过程都是在后台进行操作的,不会堵塞主线程。

Core Animation是直接作用在CALayer上,不是作用在UIView上。

核心动画可以修改动画执行的时间

核心动画结束后会回到原来的位置

1、基本动画(CABasicAnimation)

  • KeyPath 需要修改的属性
  • fromValue 从哪里
  • toValue 到哪里
  • byValue 累加到那
  • repeatCount 重复次数
  • duration 是动画执行的时间

2、关键帧动画(CAKeyframeAnimation)

  • keyPath 需要修改的属性
  • values 放关键帧的数组
  • path 做动画的路径

3、组动画(CAAnimationGroup)

  • animations 放各种动画的数组
  • 如果我们要设置动画的执行时间和重复的次数,就是要给这个组动画进行设置的

4、转场动画(CATransition)

  • type 动画类型
  • subtype 方向
  • 基本动画是属性动画的一种
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //基本动画类型
    //1、创建动画的对象也就是你需要做什么动画
    CABasicAnimation * animation=[[CABasicAnimation alloc]init];
    //2、怎么去做动画
    //x从15到200
    animation.keyPath=@"position.x";
//    animation.fromValue=@(15);
//    animation.toValue=@(200);
    //禁止返回到原来的位置就是从自身的位置去累加x
    animation.byValue=@(5);
    //动画完成不回到原来的位置,当动画完成时,我们的layer在其最终状态中仍然可见。
    animation.fillMode=kCAFillModeForwards;
   // 当为true时,当动画的活动持续时间已经过时,动画将从渲染树中删除。默认值为YES。
    animation.removedOnCompletion=NO;//不设置这个,上面的那个就没有效果
    //添加动画,就是设置对谁去做动画
    [self.layer addAnimation:animation forKey:nil];
    
}

关键帧动画也是属性动画的一种

 

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   //创建动画
    CAKeyframeAnimation *animation =[[CAKeyframeAnimation alloc]init];
    animation.keyPath=@"position";
//    NSValue * v1=[NSValue valueWithCGPoint:CGPointMake(100, 100)];
//    NSValue * v2=[NSValue valueWithCGPoint:CGPointMake(150, 100)];
//    NSValue * v3=[NSValue valueWithCGPoint:CGPointMake(100, 150)];
//    NSValue * v4=[NSValue valueWithCGPoint:CGPointMake(150, 150)];
//    //四个关键数据
//    animation.values=@[v1,v2,v3,v4];
    //动画执行的时间
    animation.duration=5;
    //重复次数
    animation.repeatCount=INT_MAX;
    //还有就是创建个路径,让layer绕着这个路径走
    UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:YES];
    animation.path=path.CGPath;
    //添加动画
    [self.layer addAnimation:animation forKey:nil];
}

组动画中包含基本动画和关键帧动画

 

 

 //创建组动画
    CAAnimationGroup * group=[[CAAnimationGroup alloc]init];
    //创建基本动画
    CABasicAnimation * animation=[[CABasicAnimation alloc]init];
    animation.keyPath=@"transform.rotation";
    //让方块自己自旋转
    animation.byValue=@(2*M_PI);
    //创建关键帧动画
    CAKeyframeAnimation * animation1 =[[CAKeyframeAnimation alloc]init];
    //怎么做动画
    animation1.keyPath=@"position";
    //创建路径
    UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:YES];
    //设置关键帧动画的路径
    animation1.path=path.CGPath;
    //设置组动画中的动画
    group.animations=@[animation,animation1];
    //设置动画的执行时间
    group.duration=2;
    //重复次数
    group.repeatCount=INT_MAX;
    //添加动画
    [self.layer addAnimation:group forKey:nil];

转场动画,其中self.imageNumber只是为了更好的去设置图片而去定义的

 

 

- (IBAction)imageChange:(UISwipeGestureRecognizer *)sender {
    
    //创建动画
    CATransition * animation=[[CATransition alloc]init];
    //进行设置
    animation.type=@"cube";
    //rippleEffect是水滴效果没有方向,cameraIrisHollowOpen是相机效果没有方向区别,moveIn就是换张图片上一张图片变透明的效果
    //设置方向
    animation.subtype=kCATransitionFromLeft;
    if(sender.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        self.imageNumber++;
        if(self.imageNumber==8)
        {
            self.imageNumber=1;
        }
        self.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%ld",self.imageNumber]];
        //设置方向,从右往左
        animation.subtype=kCATransitionFromRight;
    }
    else
    {
        self.imageNumber--;
        if(self.imageNumber==0)
        {
            self.imageNumber=7;
        }
        //设置方向
        animation.subtype=kCATransitionFromLeft;
        self.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%ld",self.imageNumber]];
    }
    //添加动画效果
    [self.imageView.layer addAnimation:animation forKey:nil];
    
}

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值