CABasicAnimation的简单介绍

CABasicAnimation是继承自CAPropertyAnimation

CAKeyFrameAnimation也是继承自CAPropertyAnimation  

CAPropertyAnimation是继承自CoreAnimation,也就是核心动画

   

这篇文章讲的是CABasicAnimation,基础动画。

你可以跳到CABasicAnimation的头文件可以看到,这个类本身只有三种属性,其余的全是继承自父类的

@property(nullable, strong) id fromValue;   动画的起始值

@property(nullable, strong) id toValue;     动画的终值

@property(nullable, strong) id byValue;     动画的起始和终值(我的理解,一个过程)

   


首先创建一个view,显示动画的一个view


UIView *animView = [[UIView alloc]init];

animView.backgroundColor = [UIColor redColor];

animView.frame =CGRectMake(0, 0, 100, 100);

animView.center = CGPointMake(self.view.center.x, self.view.center.y-100);

_animView = animView;

[self.view addSubview:animView];

  

  

点击的buttonview来动画

UIButton *animButton = [UIButton buttonWithType:UIButtonTypeCustom];

[animButton setTitle:@"让其动画" forState:UIControlStateNormal];

animButton.backgroundColor = [UIColor greenColor];

[animButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[animButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

animButton.frame = CGRectMake(0, 0, 100, 30);

animButton.center = CGPointMake(self.view.center.x, self.view.center.y+100);

_animButton = animButton;

[self.view addSubview:animButton];


首先先详细介绍其中的一个动画

位移的动画

-(void)positionAnimation{

  

调用父类的父类的CAAnimation方法 创建一个基础动画

CABasicAnimation *anima = [CABasicAnimation animation];

  

调用父类CAPropertyAnimation的方法 设置动画的方式 是平移还是旋转等等

这个不能用byValue来平移

anima.keyPath = @"transform.translation.y";


以下两种可以byValue来平移

anima.keyPath = @"position";

anima.keyPath = @"transform.translation";

  

设置初始值

100代表self.animView.layery+100 作为初始值

一般 我们设置为0 从原来的位置进行移动,这个值可以是负值

anima.fromValue =@(0);

  

设置动画停止的时候的终值

200代表self.animView.layery+100 作为初始值

这个值可以是负值

anima.toValue = @(200);

  

byValue的属性 其实 就是个路径,这个pointxy正负值代表方向,数值代表大小

相对于原来的左边移动的距离和方向,只不过在某种情况下这一个属性可以替代toValuefromValue

这的属性得是有xy 若只有单方向比如说transform.translation.y是没有动画的

这个必须是两个方向的keyPath比如position或者transform.translation等

NSValue *byValue = [NSValue valueWithCGPoint:CGPointMake(-100, -100)];

anima.byValue = byValue;


设置动画的时间 CAAnimation 协议

anima.duration = 3;

  

  

设置 动画完毕后是否回到最初的位置,也就是动画之前的位置

默认是yes

anima.removedOnCompletion = YES;

下面的方法中的key可以加 也可以不加,加的话就是为了方便以后根据这个key来找到这个动画

一般不加

[self.animView.layer addAnimation:anima forKey:@"positionAnimation"];

  

}



以下是自己写的demo动画 有兴趣的可以看一看


透明度动画

-(void)opacityAnimation{

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"opacity";

anim.fromValue = @(1);

anim.toValue = @(0.3);

anim.duration = 2;

[self.animView.layer addAnimation:anim forKey:@"opacity"]; 

}

缩放动画

-(void)scaleAnimation{

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"transform.scale";

anim.fromValue = @(0.5);//先缩小

anim.toValue = @(1.5);

anim.duration = 2;

[self.animView.layer addAnimation:anim forKey:@"transform.scale"];  

}

旋转动画

-(void)rotateAnimation{

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"transform.rotation.z";

anim.fromValue = @(0);

anim.toValue = @(M_PI);

anim.duration = 2;

[self.animView.layer addAnimation:anim forKey:@"transform.rotation.z"];  

}

边线宽度的动画

-(void)borderWidthAnimation{

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"borderWidth";

anim.fromValue = @(0);

anim.toValue = @(10);

anim.duration = 2;

[self.animView.layer addAnimation:anim forKey:@"borderWidth"];  

}

还有一些keyPath 有兴趣的童鞋可以自己研究研究,很好玩的

opacity    

margin

backgroundColor    

cornerRadius   

borderWidth

bounds

contents

contentsRect

frame

hidden

mask

masksToBounds

opacity

position

shadowColor

shadowOffset

shadowOpacity

shadowRadius


点击button的方法

-(void)clickButton:(UIButton *)button{

  

  //位移变化

//  [self positionAnimation];


  //透明度动画

//  [self opacityAnimation];

  

  //缩放动画

//  [self scaleAnimation];

  

  //旋转动画

//  [self rotateAnimation];

  

  //边线宽度的动画

  [self borderWidthAnimation];

  

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值