這篇把 iOS 上幾種常見的動畫與繪圖方式整理成一份可查閱的筆記,涵蓋 UIView 動畫、UIImageView 逐幀動畫、CATransition 轉場、Core Graphics 繪圖,以及核心動畫的關鍵幀、基礎動畫與動畫群組。
先設定動畫的代理對象為 viewController,接著依序設定各項參數:
[UIView beginAnimations:nil context:NULL]; // 動畫開始
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 加減速曲線
[UIView setAnimationDuration:1.0]; // 持續時間
[UIView setAnimationRepeatCount:2]; // 重複次數
[UIView setAnimationRepeatAutoreverses:YES]; // 是否反向播放
// 在這裡描述 view 的最終狀態
[UIView commitAnimations]; // 提交動畫
Block 語法比 delegate 直覺得多,是目前的首選:
[UIView animateWithDuration:2
animations:^{
baseView.alpha = 0;
}
completion:^(BOOL finished) {
NSLog(@"end");
}];
① 建立 UIImageView 物件
② 準備一個陣列,放入多張 UIImage
③ 把陣列指定給 animationImages
④ 設定 animationDuration 後呼叫 startAnimating
imgView.animationImages = mArr;
imgView.animationDuration = 2.0;
[imgView startAnimating];
① 建立 CATransition 物件
② 設定 duration、timingFunction、type、subType
③ 把動畫加到目標 view 的 layer 上
常見的 type 如下(部分為私有字串,正式產品請避開):
animation.type = @"cube"; // 立方體翻轉
animation.type = @"suckEffect"; // 收縮,像布被抽走
animation.type = @"oglFlip"; // 翻轉
animation.type = @"rippleEffect"; // 水波漣漪
animation.type = @"pageCurl"; // 向上翻頁
animation.type = @"pageUnCurl"; // 向下翻頁
例如替換整個導航列畫面:
[self.navigationController.view.layer addAnimation:transition forKey:@"animation"];
在 view 的子類別中覆寫 drawRect:,系統會在需要重繪時自動呼叫:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
}
任何圖形都先取得畫布(圖形上下文):
CGContextRef context = UIGraphicsGetCurrentContext();
畫筆屬性可以自由設定:
// 虛線
CGFloat arr[] = {10, 5};
CGContextSetLineDash(context, 0, arr, 2);
// 線寬
CGContextSetLineWidth(context, 2);
// 線條顏色
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// 填滿顏色
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
// 最後描繪並加入畫布
CGContextDrawPath(context, kCGPathEOFillStroke);
直線:先定位起點,再用 AddLineToPoint 連續連接即可構成多段折線。
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 220, 20);
矩形:
CGContextAddRect(context, CGRectMake(20, 20, 100, 100));
曲線與扇形:
// 三次貝茲曲線
CGContextMoveToPoint(context, 10, 100);
CGContextAddCurveToPoint(context, 55, 0, 145, 200, 200, 100);
// 二次貝茲曲線
CGContextMoveToPoint(context, 10, 200);
CGContextAddQuadCurveToPoint(context, 105, 0, 200, 200);
// 扇形
CGContextAddArc(context, 100, 100, 100, arc(90), arc(0), 1);
圖片:若不想手寫路徑,可以用 PaintCode 這類工具畫好後直接產生對應的 Core Graphics 程式碼貼回專案。
圓角必須作用在 layer 上。要注意的是,同時設定 cornerRadius 與 masksToBounds 之後,陰影會被一起裁掉,兩者無法並存:
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
// 陰影相關
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowOffset = CGSizeMake(2, 2);
先建立路徑,再讓物件沿著路徑移動:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, CGRectMake(10, 10, 200, 200));
CAKeyframeAnimation *animation =
[CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
animation.duration = 2;
animation.repeatCount = 1000;
[lView.layer addAnimation:animation forKey:@"animation"];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
basicAnimation.fromValue = @0;
basicAnimation.toValue = @(2 * M_PI);
把多個動畫打包,可以讓位移與旋轉同時發生:
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[animation, basicAnimation];
animationGroup.duration = 2;
animationGroup.repeatCount = 1000;
[lView.layer addAnimation:animationGroup forKey:@"animationGroup"];
小結一下:UIView 動畫適合簡單的屬性變化,Core Graphics 負責靜態自繪,而 CA 系列動畫才是做複雜軌跡與組合效果的主力。實際開發時,先想清楚要動的是「整個 view」還是「layer 屬性」,選錯工具往往比語法不熟更花時間。