CAAnimation 动画

 

-(void)changeII{

UIImageView* imageI=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.jpg"]];

[self.view addSubview:imageI];

//初始化---层

_kklayer=[[CALayer alloc]init];

// 设定frame

_kklayer.frame=CGRectMake(50, 180, 15, 30);

[self.view.layer addSublayer:_kklayer];//增加到layer层上

_kklayer.contents=(id)[UIImage imageNamed:@"petal.png"].CGImage;//添加照片

}

//在开始触摸的方法中,其他也可以

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

UITouch* touch=touches.allObjects.lastObject;

CGPoint point=[touch locationInView:self.view];

// 移动kklayer的位置

CABasicAnimation* animation=[CABasicAnimation animationWithKeyPath:@"position"];//

animation.fromValue=[NSValue valueWithCGPoint:_kklayer.position];

animation.toValue=[NSValue valueWithCGPoint:point];

 

// 旋转

CABasicAnimation* roatAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];

roatAnimation.toValue=[NSNumber numberWithFloat:10.0*M_PI];//旋转圈数

// 组合前面的动画操作 包含多种变化

CAAnimationGroup* group=[CAAnimationGroup animation];

group.speed=0.05;//float型 默认1.0 时间和速度不能同时设置

group.animations=[NSArray arrayWithObjects:animation,roatAnimation, nil];

_kklayer.position=point;

// 让动画开始动

[_kklayer addAnimation:group forKey:@"kk"];

//按照设置点运动

CAKeyframeAnimation* key=[CAKeyframeAnimation animation];

CGMutablePathRef pat=CGPathCreateMutable();

CGPathMoveToPoint(pat, nil, 50, 50);

CGPathAddLineToPoint(pat, NULL, 375-50, 50);

CGPathAddLineToPoint(pat, NULL, 375-50, 667-50);

CGPathAddLineToPoint(pat, NULL, 50,667-50);

CGPathAddLineToPoint(pat, NULL, 50, 50);

key.path=pat;

key.duration=10;

[kklayer addAnimation:key forKey:@"position"];

}

CAAnimation是所有动画的父类,负责控制动画的持续时间和速度,是个抽象类,不能直接使用,要使用它的字类

1.duration:动画持续的时间

2.repeatCount:动画重复的此时,无限循环可以设置成HUGE_VALF或MAXFLOAT

3.repeatDuriation:重复时间

4.removedOnCompletion:默认为YES,代表动画完毕后就从图层上移除,图层会恢复到动画执行前的状态,如果想要动画保持结束后的状态,就将该属性设为NO,不过还需要设置动画的fillMode为kCAFillModeForwards;

5.fillMode:决定当前对象 在非activity时间的行为

6.beginTime:设置动画延迟执行的时间:持续2秒  CACurrrentMediaTime()+2;CACurrrentMediaTime()为图层的当前时间

7.timingFuncation:控制动画的速度

8.delegate:设置代理

 

CALayer中可⽤用来做动画的属性:

anchorPoint : 锚点

backgroundColor : 背景颜⾊色

borderColor : 边框颜⾊色

borderWidth :边框宽度

bounds :图层⼤大⼩小

contents :图层显⽰示内容

contentsRect : 图层显⽰示内容的⼤大⼩小和位置

cornerRadius : 圆⾓角半径

hidden :是否隐藏

mask : 图层蒙版

masksToBounds : 是否剪切图层边界,默认为NO

opacity: 透明度 ,类似于UIView的alpha

position : 图层中⼼心点位置,类似于UIView的center

shadowColor : 阴影颜⾊色

shadowOffset : 阴影偏移量

shadowOpacity : 阴影透明度

shadowPath : 阴影的形状

shadowRadius : 阴影模糊半径

sublayers : ⼦子图层

sublayerTransform : ⼦子图层形变

transform : 图层形变

zPosition: 图层的重叠顺序

基本动画CABasicAnimation

CABasicAnimation 用于实现layer属性值从一个值(fromValue)到另外一个值(toValue)变化的简单动画,比如旋转、缩放、逐渐透明、移动等。

动画的属性

  • fromValue:属性的起始值,id类型。
  • toValue:属性的目标值,id类型。
  • byValue:属性要变化的值,也就是fromValue到toValue所改变的值。
  • duration:动画的持续时间。
  • autoreverses:动画是否有恢复动画。
  • repeatCount:动画重复次数,设置为HUGE_VALF表示无限次。
  • removedOnCompletion:是否回到初始状态,要设置fillMode为kCAFillModeForwards才起效果。
  • fillMode:非动画时的状态。
  • speed:动画的速度,默认1.0,设为2.0就是两倍的速度完成动画。
  • timingFunction:动画速度变化控制函数包含(Linear、EaseIn、EaseIn、EaseInEaseOut)。
  • timeOffset:动画的时间偏移。
  • beginTime:动画开始的时间,为CACurrentMediaTime() + 延迟秒数。
  • CACurrentMediaTime():当前媒体时间,可以用于动画暂停和开始的时间记录。这个绝对时间就是将mach_absolute_time()(mach_absolute_time是一个CPU/总线依赖函数,返回一个基于系统启动后的时钟”嘀嗒”数。)转换成秒后的值.这个时间和系统的uptime有关,系统重启后CACurrentMediaTime()会被重置。

转载于:https://my.oschina.net/131438/blog/741210

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值