一、position和anchorPoint
position:用來設置CALayer在父層中的位置,以父層的左上角為原點(0, 0)
anchorPoint(錨點):
稱為“定位點”、“錨點”
決定著CALayer身上的哪個點會在position屬性所指的位置
以自己的左上角為原點(0, 0)
它的x、y取值范圍都是0~1,默認值為(0.5, 0.5)
推薦一個連接:http://www.cnblogs.com/wendingding/p/3800736.html講的非常詳細,而且有圖視,默認的錨點為中心點:(0.5,0.5),如果重新設置了錨點,運行動畫的時候會發現整個控件移動了,所以在設置錨點的時候需要重新設置position,
CGPoint oldAnchorPoint = _homeBtn.layer.anchorPoint;
_homeBtn.layer.anchorPoint =CGPointMake(0.5,0);
[_homeBtn.layersetPosition:CGPointMake(_homeBtn.layer.position.x + _homeBtn.layer.bounds.size.width * (_homeBtn.layer.anchorPoint.x - oldAnchorPoint.x),_homeBtn.layer.position.y +_homeBtn.layer.bounds.size.height * (_homeBtn.layer.anchorPoint.y - oldAnchorPoint.y))];
二:CABasicAnimation的使用
當你創建一個 CABasicAnimation 時,你需要通過-setFromValue 和-setToValue 來指定一個開始值和結束值。 當你增加基礎動畫到層中的時候,它開始運行。
Autoreverses
當你設定這個屬性為 YES 時,在它到達目的地之後,動畫的返回到開始的值,代替了直接跳轉到 開始的值。
Duration
Duration 這個參數你已經相當熟悉了。它設定開始值到結束值花費的時間。期間會被速度的屬性所影響。 RemovedOnCompletion
這個屬性默認為 YES,那意味著,在指定的時間段完成後,動畫就自動的從層上移除了。這個一般不用。
假如你想要再次用這個動畫時,你需要設定這個屬性為 NO。這樣的話,下次你在通過-set 方法設定動畫的屬 性時,它將再次使用你的動畫,而非默認的動畫。
Speed
默認的值為 1.0.這意味著動畫播放按照默認的速度。如果你改變這個值為 2.0,動畫會用 2 倍的速度播放。 這樣的影響就是使持續時間減半。如果你指定的持續時間為 6 秒,速度為 2.0,動畫就會播放 3 秒鐘---一半的 持續時間。
BeginTime
這個屬性在組動畫中很有用。它根據父動畫組的持續時間,指定了開始播放動畫的時間。默認的是 0.0.組 動畫在下個段落中討論“Animation Grouping”。
TimeOffset
如果一個時間偏移量是被設定,動畫不會真正的可見,直到根據父動畫組中的執行時間得到的時間都流逝 了。
RepeatCount
默認的是 0,意味著動畫只會播放一次。如果指定一個無限大的重復次數,使用 1e100f。這個不應該和 repeatDration 屬性一塊使用。
RepeatDuration
這個屬性指定了動畫應該被重復多久。動畫會一直重復,直到設定的時間流逝完。它不應該和 repeatCount 一起使用。
示例代碼:
CGPoint fromPoint = self.testImage.center;
//路徑曲線controlPoint為基准點
UIBezierPath * movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
CGPoint toPoint = CGPointMake(300, 460);
[movePath addQuadCurveToPoint:toPoint controlPoint:CGPointMake(300, 0)];
//關鍵幀 設置動畫路徑
CAKeyframeAnimation * moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// moveAnimation.path = movePath.CGPath;
moveAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)],[NSValue valueWithCGPoint:CGPointMake(100, 300)],[NSValue valueWithCGPoint:CGPointMake(300, 300)],[NSValue valueWithCGPoint:CGPointMake(300, 100)]];
moveAnimation.removedOnCompletion = YES;
//縮放變化
CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnimation.removedOnCompletion = YES;
// //透明度變化
// CABasicAnimation * opacityAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
// opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
// opacityAnimation.toValue = [NSNumber numberWithFloat:0.1];
// opacityAnimation.removedOnCompletion = YES;
//旋轉
CABasicAnimation * tranformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
tranformAnimation.fromValue = [NSNumber numberWithFloat:0.f];
tranformAnimation.toValue = [NSNumber numberWithFloat:M_PI];
tranformAnimation.cumulative = YES;
tranformAnimation.removedOnCompletion = YES;
CAAnimationGroup*animaGroup = [CAAnimationGroup animation];
animaGroup.animations = @[moveAnimation,scaleAnimation,tranformAnimation];
animaGroup.duration = 2.f;
可以通過改變animationWithKeyPath來改變動畫:
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor 背景顏色
cornerRadius 圓角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
58同城客戶端,tabbar的點擊的動畫效果,就可以通過設置CABasicAnimation屬性來做,個人花了一個小時的時間搞定,證明完全可以實現,
CGPoint oldAnchorPoint = _homeBtn.layer.anchorPoint;
_homeBtn.layer.anchorPoint = CGPointMake(0.5, 0);
[_homeBtn.layer setPosition:CGPointMake(_homeBtn.layer.position.x + _homeBtn.layer.bounds.size.width * (_homeBtn.layer.anchorPoint.x - oldAnchorPoint.x), _homeBtn.layer.position.y + _homeBtn.layer.bounds.size.height * (_homeBtn.layer.anchorPoint.y - oldAnchorPoint.y))];
CABasicAnimation*shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shakeAnimation.duration = 0.07;
shakeAnimation.autoreverses = YES;//當你設定這個屬性為 YES 時,在它到達目的地之後,動畫的返回到開始的值,代替了直接跳轉到 開始的值。
shakeAnimation.repeatCount = 2;
shakeAnimation.removedOnCompletion = NO;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-0.05];
shakeAnimation.toValue = [NSNumber numberWithFloat:0.05];
[self.homeBtn.layer addAnimation:shakeAnimation forKey:@"shakeAnimation"];