iOS 的動畫大致分兩條路:操作 UIView 的 animation 系列方法,以及直接使用核心動畫(Core Animation)。從 iOS 7 開始,UIView 動畫底層其實就是核心動畫,兩者並非毫無關聯。以下把常用寫法整理成清單。
核心動畫的類別關係大致如下:
用於場景切換,可為圖層提供移出/移入螢幕的效果。UINavigationController 的推入動畫正是靠它實作。
常用屬性:
type:轉場類型subtype:轉場方向startProgress:動畫起點(佔整體的百分比,0~1)endProgress:動畫終點(起點數值必須小於終點)CAPropertyAnimation 的子類,一次控制一個屬性,在 fromValue 與 toValue 之間變化。
CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];
baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];
baseAnimation.duration = 2.0;
baseAnimation.removedOnCompletion = NO;
baseAnimation.fillMode = kCAFillModeForwards;
baseAnimation.repeatCount = MAXFLOAT;
[self.myView.layer addAnimation:baseAnimation forKey:nil];
同樣是 CAPropertyAnimation 的子類,差別在於可以設定多個關鍵幀。CABasicAnimation 可以理解成只有兩個關鍵幀的特例。利用多關鍵幀能做出泡泡浮動這類效果。
CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
// 位移:沿圓形路徑移動
CAKeyframeAnimation *positionAnima =
[CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnima.calculationMode = kCAAnimationCubicPaced;
positionAnima.duration = 5;
positionAnima.fillMode = kCAFillModeForwards;
positionAnima.removedOnCompletion = NO;
positionAnima.repeatCount = MAXFLOAT;
positionAnima.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGRect circleContainer = CGRectInset(myView.frame,
myView.frame.size.width / 2 - 3,
myView.frame.size.height / 2 - 3);
CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
positionAnima.path = curvedPath;
CGPathRelease(curvedPath);
[myView.layer addAnimation:positionAnima forKey:nil];
// 水平縮放
CAKeyframeAnimation *scaleX =
[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleX.duration = 1.0;
scaleX.values = @[@1.0, @1.1, @1.0];
scaleX.keyTimes = @[@0.0, @0.5, @1.0];
scaleX.repeatCount = MAXFLOAT;
scaleX.autoreverses = YES;
scaleX.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleX forKey:nil];
// 垂直縮放
CAKeyframeAnimation *scaleY =
[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
scaleY.duration = 1.5;
scaleY.values = @[@1.0, @1.1, @1.0];
scaleY.keyTimes = @[@0.0, @0.5, @1.0];
scaleY.repeatCount = MAXFLOAT;
scaleY.autoreverses = YES;
scaleY.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleY forKey:nil];
group.animations = @[positionAnima, scaleX, scaleY];
可以保存一組動畫物件,加入圖層後同時運行。當多個動畫時長不一致時,動畫組尤其方便——上面的泡泡效果就是例子。
group.animations = @[ /* 放入各個動畫物件 */ ];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
CGPoint point = self.myView.center;
point.y += 150;
[self.myView setCenter:point];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
CGPoint point = self.myView.center;
point.y += 150;
[self.myView setCenter:point];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
[self.myView setAlpha:0.1];
[UIView commitAnimations];
[UIView animateWithDuration:4 animations:^{
CGPoint point = self.myView.center;
point.y += 150;
[self.myView setCenter:point];
}];
蘋果提供了便捷方法,不必自行建立幀動畫實例,直接控制屬性即可。
[UIView animateKeyframesWithDuration:0.5
delay:1
options:UIViewKeyframeAnimationOptionAutoreverse
animations:^{
self.view.bounds = CGRectMake(30, 30, 30, 30);
} completion:^(BOOL finished) {
}];
+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion
這個方法呼叫完成後,效果等同於執行以下兩句:
[fromView.superview addSubview:toView];
[fromView removeFromSuperview];
參數說明:
duration:動畫持續時間options:轉場動畫的類型completion:動畫結束後自動呼叫的區塊