animationWithKeyPath的值:
transform.scale =比例转换
transform.scale.x =宽的比例转换
transform.scale.y =高的比例转换
transform.rotation.z =平面圖的转换
opacity =透明度
margin =边缘
position =位置
backgroundColor =背景
cornerRadius =边缘弧度
borderWidth =边缘宽度
bounds =边界 缩放
contents =内容
contentsRect =内容矩形
frame =
hidden =隐藏
mask =
masksToBounds =
shadowColor
shadowOffset
shadowOpacity
shadowRadius
CALayer*kkLayer = [[CALayeralloc]init];
[kkLayersetFrame:CGRectMake(100,100,30,30)];
[kkLayersetBackgroundColor:[UIColorredColor].CGColor];
[kkLayersetCornerRadius:15];
[self.view.layeraddSublayer:kkLayer];
CABasicAnimation*animationBase = [CABasicAnimationanimationWithKeyPath:@"position"];
animationBase.fromValue= [NSValuevalueWithCGPoint:kkLayer.position];
CGPointtoPoint = kkLayer.position;
toPoint.x+=180;
animationBase.toValue= [NSValuevalueWithCGPoint:toPoint];
//以x轴进行旋转
CABasicAnimation*rotateAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.x"];
rotateAnimation.fromValue= [NSNumbernumberWithFloat:0.0];
rotateAnimation.toValue= [NSNumbernumberWithFloat:6.0*M_PI];
//对kkLayer进行放大缩小
CABasicAnimation*scaoleAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale.x"];
scaoleAnimation.duration=3;
scaoleAnimation.autoreverses=YES;
scaoleAnimation.fromValue= [NSNumbernumberWithFloat:1.0];
scaoleAnimation.toValue= [NSNumbernumberWithFloat:2.5];
scaoleAnimation.fillMode=kCAFillModeForwards;
CABasicAnimation*hidenBase = [CABasicAnimationanimationWithKeyPath:@"hidden"];
hidenBase.fromValue= [NSNumbernumberWithBool:NO];
hidenBase.toValue= [NSNumbernumberWithBool:YES];
//把上面的动画组合起来
CAAnimationGroup*group = [CAAnimationGroupanimation];
// group.autoreverses = NO;
//完成后反向完成
group.duration=3.0;
group.animations= [NSArrayarrayWithObjects:animationBase,rotateAnimation, scaoleAnimation,hidenBase,nil];
group.repeatCount=NSNotFound;
// PS:动画结束以后,他会返回到自己原来的frame,如果不想到原来frame我们需要设定
group.fillMode=kCAFillModeForwards;
[kkLayeraddAnimation:groupforKey:@"kkLayerMove"];
属性
@property(nullable,strong)idfromValue;
@property(nullable,strong)idtoValue;
@property(nullable,strong)idbyValue;
byValue和toValue的区别,前者是在当前的位置上增加多少,后者是到指定的位置。
实例
1.位移
CALayer *layer = _bgView.layer;
layer.anchorPoint= CGPointMake(0,0);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue= [NSValue valueWithCGPoint:CGPointMake(100,100)]; //开始的值
basicAnimation.toValue= [NSValue valueWithCGPoint:CGPointMake(250,250)]; //移动的值
basicAnimation.duration=1.0f;//默认是0.25秒
basicAnimation.fillMode= kCAFillModeForwards; //设置保存动画的最新状态
basicAnimation.removedOnCompletion= NO;//不恢复原来的位置
[layer addAnimation:basicAnimation forKey:@"positionAnimation"];
2.变色
CALayer *layer = _bgView.layer; layer.anchorPoint= CGPointMake(0,0);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
basicAnimation.fromValue= (id)[UIColorredColor].CGColor;
basicAnimation.toValue= (id)[UIColoryellowColor].CGColor;
basicAnimation.duration=1.0f;
basicAnimation.fillMode= kCAFillModeForwards; //设置保存动画的最新状态
basicAnimation.removedOnCompletion=NO;//不恢复原来的位置
[layer addAnimation:basicAnimation forKey:@"backgroundAnimation"];
3.缩放
CALayer *layer = _bgView.layer;
layer.anchorPoint= CGPointMake(0.5,0.5);//在anchor指定的点在point上,默认是中点在point指定的点上
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue= [NSNumbernumberWithDouble:1.0];
animation.toValue= [NSNumbernumberWithDouble:2.0];
[layer addAnimation:animation forKey:@"scaleAnimation”];
4.旋转
CALayer *layer = _bgView.layer;
layer.anchorPoint= CGPointMake(0.5,0.5);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//绕z轴旋转
animation.toValue= [NSNumbernumberWithDouble:2*M_PI];
animation.duration=1.0; [layer addAnimation:animation forKey:@"rotationAnimation"];
网友评论