基礎(chǔ)動(dòng)畫,話不多說,直接來看例子:
UIView *animatView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
animatView.backgroundColor = [UIColor redColor];
[self.view addSubview:animatView];
CABasicAnimation *basic = [[CABasicAnimation alloc] init];
basic.keyPath = @"position";
basic.repeatCount = MAXFLOAT;
basic.duration = 3;
basic.beginTime = CACurrentMediaTime() + 2;
basic.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 600)];
basic.removedOnCompletion = NO;
basic.fillMode = kCAFillModeForwards;
[animatView.layer addAnimation:basic forKey:nil];
效果圖如下:
下面我們來分析一下:
首先需要注意一點(diǎn),所有這些屬性的設(shè)置都必須在 給 layer 加入動(dòng)畫以前設(shè)置,
basic.keyPath 是指他的位置的變化
animationWithKeyPath的值:
transform.scale = 比例轉(zhuǎn)換
transform.scale.x = 闊的比例轉(zhuǎn)換
transform.scale.y = 高的比例轉(zhuǎn)換
transform.rotation.z = 平面圖的旋轉(zhuǎn)
opacity = 透明度
margin
zPosition
backgroundColor 背景顏色
cornerRadius 圓角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
basic.beginTime 是 動(dòng)畫多長時(shí)間開始執(zhí)行,意思就是延遲多長時(shí)間(一定要注意我這這個(gè)時(shí)間的設(shè)置方法)
basic.repeatCount 重復(fù)次數(shù)
basic.duration 間隔
basic.removedOnCompletion 動(dòng)畫完成后是否移除,NO就是不移除
basic.fillMode 動(dòng)畫完成后的狀態(tài),要配合上一個(gè)屬性使用
我們還注意到 CAAnimation 自己的特有的屬性
fromValue, toValue, byValue
fromValue:初始狀態(tài),從這個(gè)狀態(tài)開始
toValue : 結(jié)束狀態(tài), 到這個(gè)狀態(tài)停止(完成一次動(dòng)畫)
byValue :每回增加多少,狀態(tài)的增量
我們?cè)賮碜鲆粋€(gè)例子:
UIView *animatView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:animatView];
CABasicAnimation *basic = [[CABasicAnimation alloc] init];
basic.keyPath = @"contents";
basic.fromValue = (id)[UIImage imageNamed:@"01.jpeg"].CGImage;
basic.toValue = (id)[UIImage imageNamed:@"02.jpeg"].CGImage;
basic.duration = 2;
basic.removedOnCompletion = NO;
basic.fillMode = kCAFillModeForwards;
basic.repeatCount = MAXFLOAT;
[animatView.layer addAnimation:basic forKey:nil];
效果圖:
這里的keyPath 是 "contents",注意因?yàn)槭亲址欢ú荒芷村e(cuò)了