所有核心動畫的動畫類都是從 CAAnimation類繼承而來
CAAnimation 實現了 CAMediaTiming協議,提供了動畫的持續時間,速度,和重複計數
CAAnimation 也實現了 CAAction協議。該協議爲圖層觸發一個動畫動作提供了提供 標準化響應
CATransition 提供了一個圖層變化的過渡效果,它能影響圖層的整個內容。 動畫進行的時候淡入淡出(fade)、推(push)、顯露(reveal)圖層的內容。這些過渡效果可以擴展到你自己定製的Core Image 濾鏡
CAAnimationGroup 允許一系列動畫效果組合在一起,並行顯示動畫
CAPropertyAnimation是一個抽象的子類,它支持動畫的顯示圖層的關鍵路徑中指定的屬性
CABasicAnimation 簡單的爲圖層的屬性提供修改。
CAKeyframeAnimation支持關鍵幀動畫,你可以指定的圖層屬性的關鍵路徑動畫,包括動畫的每個階段的價值,以及關鍵幀時間和計時功能的一系列值。在 動畫運行是,每個值被特定的插入值替代
核心動畫的 CAConstraint 類 是一個佈局管理器,它可以指定子圖層類限制於你指定的約束集合。每個約束(CAConstraint類的實例封裝)描述層的幾何屬性(左,右,頂部或底部的邊緣或水平或垂直中心)的關係,關係到其同級之一的幾何屬性層或superlayer
核心動畫的圖層和 Cocoa 的視圖在很大程度上沒有一定的相似性,但是他們兩者最大的區別是,圖層不會直接渲染到屏幕上
試圖的position座標和anchorPoint有關
sublayerTransform屬性指定的矩陣只會影響圖層的子圖層,而不會對 圖層本身產生影響
transform 屬性指定的矩陣結合圖層的anchorPoint屬性作用於圖層和圖層的子圖層上 面
旋轉的單位採用弧度(radians),而不是角度(degress)。以下兩個函數,你可以在 弧度和角度之間切換
- <span style="font-size:18px;">CGFloat DegreesToRadians(CGFloat degrees) {returndegrees * M_PI / 180;};
- CGFloat RadiansToDegrees(CGFloat radians) {returnradians * 180 / M_PI;};</span>
無法正常運行:
替換的辦法是,你必須通過 setValue:forKeyPath:或者valueForKeyPath:方法,具體如下:
- <span style="font-size:18px;">myLayer.transform.rotation.x=0;
- [myLayer setValue:[NSNumber numberWithInt:0]forKeyPath:@"transform.rotation.x"];</span>
如果一個圖層的屬性 needsDisplayOnBoundsChan
給CALayer提供內容
1。包含圖片內容的 CGImageRef來顯式的設置圖層的contents 的屬性。
2。指定一個委託,它提供或者重繪內容。
3。繼承 CALayer 類重載顯示的函數
創建一個委託類實
displayLayer:或 drawLayer:inContext:
- <span style="font-size:18px;">- (void)displayLayer:(CALayer *)theLayer
- {
- // check the value of the layer's state key
- if ([[theLayer valueForKey:@"state"] boolValue])
- {
- // display the yes image
- theLayer.contents=[someHelperObjectloadStateYesImage];
- }
- else {
- // display the no image
- theLayer.contents=[someHelperObjectloadStateNoImage];
- }
- }</span>
如果你必須重繪圖層的內容,而不是通過加載圖片,那你需要實現drawLayer:inContext:方法。通過委託可以決定哪些內容是需要的並使用CGContextRef來重繪內容。
實例重新緩存其內容,可以通過發送以下任何一個方法setNeedsDisplay或setNeedsDisplayInRect:的消息,亦或者設置圖層的needsDisplaOnBoundsChang
CALayer 的屬性 contentsGravity允許你在圖層的邊界內容修改圖層的contents圖片的位置或者伸縮值。默認情況下,內容的圖像完全填充層的邊界,忽視自然的圖像 寬高比contentsGravity屬性值:
CABasicAnimation提供了在圖層的屬性值間簡單的插入。CAKeyframeAnimation提供支持關鍵幀動畫。你指定動畫的一個圖層屬性的關鍵路徑,一個表示在動畫的每個階段的價值的數組,還有一個關鍵幀時間的數組和時間函數。
CATransition提供了一個影響整個圖層的內容過渡效果。在動畫顯示過程中採用淡出(fade)、推出(push)、顯露(reveal)圖層的內容。常用的過渡效果可以通過提供你自己定製的核心圖像濾鏡來擴展
- <span style="font-size:18px;">CABasicAnimation *_basicAnimation= [CABasicAnimation animationWithKeyPath:@"opacity"];
- _basicAnimation.duration=3.0;
- _basicAnimation.repeatCount=2;
- _basicAnimation.autoreverses=YES;
- _basicAnimation.fromValue=[NSNumber numberWithFloat:1.0];
- _basicAnimation.toValue=[NSNumber numberWithFloat:0.0];
- [self.m_imageViewBg.layeraddAnimation:_basicAnimation forKey:@"animateOpacity"];</span>
我們可以通過animationWithKeyPath鍵值對的方式來改變動畫
animationWithKeyPath的值:
transform.scale= 比例轉換
transform.scale.x =闊的比例轉換
transform.scale.y =高的比例轉換
transform.rotation.z =平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor
cornerRadius
bounds
contents
- <span style="font-size:18px;">
- [self. ui_View.layerremoveAllAnimations];
- CABasicAnimation *pulse= [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- pulse.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
- pulse.duration = 0.5 + (rand() % 10) * 0.05;
- pulse.repeatCount = 1;
- pulse.autoreverses = YES;
- pulse.fromValue =[NSNumber numberWithFloat:.8];
- pulse.toValue =[NSNumber numberWithFloat:1.2];
- [self.ui_View.layer addAnimation:pulseforKey:nil];
- // bounds
- CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"bounds"];
- anim.duration = 1.f;
- anim.fromValue =[NSValue valueWithCGRect:CGRectMake(0,0,10,10)];
- anim.toValue =[NSValue valueWithCGRect:CGRectMake(10,10,200,200)];
- anim.byValue = [NSValuevalueWithCGRect:self. ui_View.bounds];
- // anim.toValue= (id)[UIColor redColor].CGColor;
- // anim.fromValue = (id)[UIColorblackColor].CGColor;
- anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- anim.repeatCount = 1;
- anim.autoreverses = YES;
- [ui_View.layeraddAnimation:anim forKey:nil];
- //cornerRadius
- CABasicAnimation*anim2 = [CABasicAnimationanimationWithKeyPath:@"cornerRadius"];
- anim2.duration = 1.f;
- anim2.fromValue =[NSNumber numberWithFloat:0.f];
- anim2.toValue =[NSNumber numberWithFloat:20.f];
- anim2.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- anim2.repeatCount = CGFLOAT_MAX;
- anim2.autoreverses = YES;
- [ui_View.layeraddAnimation:anim2 forKey:@"cornerRadius"];
- //contents
- CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"contents"];
- anim.duration = 1.f;
- anim.fromValue =(id)[UIImageimageNamed:@"1.jpg"].CGImage;
- anim.toValue =(id)[UIImageimageNamed:@"2.png"].CGImage;
- // anim.byValue = (id)[UIImageimageNamed:@"3.png"].CGImage;
- // anim.toValue= (id)[UIColor redColor].CGColor;
- // anim.fromValue = (id)[UIColorblackColor].CGColor;
- anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- anim.repeatCount = CGFLOAT_MAX;
- anim.autoreverses = YES;
- [ui_View.layeraddAnimation:anim forKey:nil];
- [ui_View.layersetShadowOffset:CGSizeMake(2,2)];
- [ui_View.layersetShadowOpacity:1];
- [ui_View.layersetShadowColor:[UIColorgrayColor].CGColor];
- //
- CABasicAnimation *anim =[CABasicAnimation animationWithKeyPath:@"shadowColor"];
- anim.duration = 1.f;
- anim.toValue =(id)[UIColorredColor].CGColor;
- anim.fromValue = (id)[UIColorblackColor].CGColor;
- anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- anim.repeatCount = CGFLOAT_MAX;
- anim.autoreverses = YES;
- [ui_View.layeraddAnimation:anim forKey:nil];
- CABasicAnimation *_anim= [CABasicAnimation animationWithKeyPath:@"shadowOffset"];
- _anim.duration = 1.f;
- _anim.fromValue =[NSValue valueWithCGSize:CGSizeMake(0,0)];
- _anim.toValue =[NSValue valueWithCGSize:CGSizeMake(3,3)];
- _anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- _anim.repeatCount = CGFLOAT_MAX;
- _anim.autoreverses = YES;
- [ui_View.layeraddAnimation:_anim forKey:nil];
- CABasicAnimation *_anim1= [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
- _anim1.duration = 1.f;
- _anim1.fromValue =[NSNumber numberWithFloat:0.5];
- _anim1.toValue =[NSNumber numberWithFloat:1];
- _anim1.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- _anim1.repeatCount = CGFLOAT_MAX;
- _anim1.autoreverses = YES;
- [ui_View.layeraddAnimation:_anim1 forKey:nil];
- CABasicAnimation *_anim2= [CABasicAnimation animationWithKeyPath:@"shadowRadius"];
- _anim2.duration = 1.f;
- _anim2.fromValue =[NSNumber numberWithFloat:10];
- _anim2.toValue =[NSNumber numberWithFloat:5];
- _anim2.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- _anim2.repeatCount = CGFLOAT_MAX;
- _anim2.autoreverses = YES;
- [ui_View.layeraddAnimation:_anim2 forKey:nil];</span>
CAScrollLayer 對象的滾動區域的範圍在它的子圖層裏面定義。
CAScrollLaye 不提供鍵盤或鼠標事件處理,也不提供可見的滾動條。
CATextLayer可以方便的從字符串或字符串的內容創建一個圖層類的內容
CATiledLayer 允許遞增的顯示大而複雜的圖片
CAEAGLLayer 提供了一個OpenGLES渲染環境
CALayer 的還擴展了 NSKeyValueCoding的非正式協議,加入默認鍵值和額外的結構類型的自動對象包裝(CGPoint,CGSize,CGRect,CGAffineTransform和CATransform3D)的支持,並提供許多這些結構的關鍵路徑領域的訪問