Professional Documents
Culture Documents
CoreAnimation
Weizhong Yang
a.k.a zonble
zonble@gmail.com
http://zonble.net
https://github.com/zonble/CAShowcase
New BSD License
• 這年頭動畫效果非常重要,尤其是 iPad
• 當 UIView 提供的動畫不夠用的時候,又
還不需要用到自己寫 OpenGL,就該用
CoreAnimation 了。
• 一套 Mac/iOS framework
• 製作各種平面動畫
• UIView 動畫其實算是 CoreAnimation 的
wrapper
• 難的往往是設計與溝通
• 在設計階段很難用 Mockup 溝通-怎麼
講都很難讓對方理解最後會產生什麼動
畫,除非把程式寫出來…
• 如果不是對方要的,那程式就白寫了…
• 都是 Objective-C 物件
• 可喜可賀
• CALayer:演員
• CAAnimation:腳本
• CATransaction:整體情境設定
• #import <QuartzCore/QuartzCore.h>
• iOS
• 每個 UIView 都有一個 layer([aView
layer] or aView.layer)
• Mac
•NSView 要特別設定 setWantsLayer:
• 今天以 iOS 為主
• 有 frame、bounds、background color
• 把一個 layer 放到另外一個 layer 上面,
是用 addSublayer
• 加入之後,也可以知道自己的 superlayer
• 不是 responder,不處理各種 event
(Click、Touch…)
• 大部分 property 一改就會產生動畫
• 繪製內容的方法也稍微不一樣
• aLayer.position = CGPointMake()...
• aLayer.bounds = CGRectMake()...
• 都會直接產生動畫效果
• 預設動畫時間為 0.25 秒
• CABasicAnimation
• CAKeyframeAnimation
• CAAnimationGroup
• CATransition …前面講了
• 還有
• @”transform.rotation.x”
• @”transform.rotation.y”
• @”transform.rotation.z”
• ...
• fromValue = [NSValue...];
• toValue = [NSValue ...];
• duration = 3.0; //Seconds
• repeatCount = NSUIntegerMax; // Forever
• 不是用 fromValue、toValue,而是用
CGPathRef
• CAKeyframeAnimation *a =
[CAKeyframeAnimation
animationWithKeyPath:@"position"];
• a.path = (CGPathRef)path;
• CAAnimationGroup *group =
[CAAnimationGroup animation];
• group.animations = [NSArray
arrayWithObjects:anim1, anim2, nil];
• [aLayer addAnimation:myAnimation
forKey:@"MyKey"];
一個動畫做完
再做一個動畫
?
...
// 目前還沒想到更好的辦法
• 一個 CAAnimation 結束的時候應該要有
block 可以跑啊啊啊啊啊啊啊…
• iOS 5 會有嗎?
• Lion 好像還沒看到
• UILayer -layoutSublayers
• 我也常用 UIView -layoutSubviews 就是
了…
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];
UIImage *anImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return anImage;
// 這個很好用 :D