在創(chuàng)建UIView對象時,UIView內(nèi)部會自動創(chuàng)建一個圖層(即CALayer對象),通過UIView的layer屬性可以訪問這個層
當(dāng)UIView需要顯示到屏幕上時,會調(diào)用drawRect:方法進行繪圖,并且會將所有內(nèi)容繪制在自己的圖層上,繪圖完畢后,系統(tǒng)會將圖層拷貝到屏幕上,于是就完成了UIView的顯示
換句話說,UIView本身不具備顯示的功能,是它內(nèi)部的層才有顯示功能
//寬度和高度@property CGRect bounds;//位置(默認指中點,具體由anchorPoint決定)@property CGPoint position;//錨點(x,y的范圍都是0-1),決定了position的含義@property CGPoint anchorPoint;//背景顏色(CGColorRef類型)@property CGColorRef backgroundColor;//形變屬性@property CATransform3D transform;//邊框顏色(CGColorRef類型)@property CGColorRef borderColor;//邊框?qū)挾?/span>@property CGFloat borderWidth;//內(nèi)容(比如設(shè)置為圖片CGImageRef)@property(retain) id contents;
其次QuartzCore框架和CoreGraphics框架是可以跨平臺使用的,在iOS和Mac OS X上都能使用 但是UIKit只能在iOS中使用
為了保證可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
CALayer有2個非常重要的屬性:position和anchorPoint @property CGPoint position; //用來設(shè)置CALayer在父層中的位置 //以父層的左上角為原點(0, 0) @property CGPoint anchorPoint; //稱為“定位點”、“錨點” //決定著CALayer身上的哪個點會在position屬性所指的位置 //以自己的左上角為原點(0, 0) //它的x、y取值范圍都是0~1,默認值為(0.5, 0.5) 示意圖:


每一個UIView內(nèi)部都默認關(guān)聯(lián)著一個CALayer,我們可用稱這個Layer為Root Layer(根層)
所有的非Root Layer,也就是手動創(chuàng)建的CALayer對象,都存在著隱式動畫
什么是隱式動畫?
當(dāng)對非RootLayer的部分屬性進行修改時,默認會自動產(chǎn)生一些動畫效果而這些屬性稱為Animatable Properties(可動畫屬性)
列舉幾個常見的Animatable Properties: bounds:用于設(shè)置CALayer的寬度和高度。修改這個屬性會產(chǎn)生縮放動畫 backgroundColor:用于設(shè)置CALayer的背景色。修改這個屬性會產(chǎn)生背景色的漸變動畫 position:用于設(shè)置CALayer的位置。修改這個屬性會產(chǎn)生平移動畫
可以通過動畫事務(wù)(CATransaction)關(guān)閉默認的隱式動畫效果 [CATransaction begin]; [CATransaction setDisableActions:YES]; self.myview.layer.position = CGPointMake(10, 10); [CATransaction commit];
CAAnaimation是個抽象類,不具備動畫效果,必須用它的子類才有動畫效果,子類是CAAnimationGroup和CATransition才有動畫效果
CAAnimationGroup是個動畫組,可以同時進行縮放,旋轉(zhuǎn)
CATransition是轉(zhuǎn)場動畫,界面之間跳轉(zhuǎn)都可以用轉(zhuǎn)場動畫

使用UIView動畫函數(shù)實現(xiàn)轉(zhuǎn)場動畫——單視圖
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; 參數(shù)說明: duration:動畫的持續(xù)時間 view:需要進行轉(zhuǎn)場動畫的視圖 options:轉(zhuǎn)場動畫的類型 animations:將改變視圖屬性的代碼放在這個block中 completion:動畫結(jié)束后,會自動調(diào)用這個block
使用UIView動畫函數(shù)實現(xiàn)轉(zhuǎn)場動畫——雙視圖
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;參數(shù)說明:duration:動畫的持續(xù)時間options:轉(zhuǎn)場動畫的類型animations:將改變視圖屬性的代碼放在這個block中completion:動畫結(jié)束后,會自動調(diào)用這個block
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; anim.toValue = @0.5; anim.repeatCount = MAXFLOAT; [_imageV.layer addAnimation:anim forKey:nil]; }
動畫過程說明: + 隨著動畫的進行,在長度為duration的持續(xù)時間內(nèi),keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue + keyPath內(nèi)容是CALayer的可動畫Animatable屬性 + 如果fillMode=kCAFillModeForwards同時removedOnComletion=NO,那么在動畫執(zhí)行完畢后,圖層會保持顯示動畫執(zhí)行后的狀態(tài)。但在實質(zhì)上,圖層的屬性值還是動畫執(zhí)行前的初始值,并沒有真正被改變 + CAKeyframeAnimation幀動畫,做一些連續(xù)的流暢的動畫 - 關(guān)鍵幀動畫,也是CAPropertyAnimation的子類,與CABasicAnimation的區(qū)別是:CABasicAnimation只能從一個數(shù)值(fromValue)變到另一個數(shù)值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數(shù)值 - 屬性說明: + values:上述的NSArray對象。里面的元素稱為“關(guān)鍵幀”(keyframe)。動畫對象會在指定的時間(duration)內(nèi),依次顯示values數(shù)組中的每一個關(guān)鍵幀 + path:可以設(shè)置一個CGPathRef、CGMutablePathRef,讓圖層按照路徑軌跡移動。path只對CALayer的anchorPoint和position起作用。如果設(shè)置了path,那么values將被忽略 + keyTimes:可以為對應(yīng)的關(guān)鍵幀指定對應(yīng)的時間點,其取值范圍為0到1.0,keyTimes中的每一個時間值都對應(yīng)values中的每一幀。如果沒有設(shè)置keyTimes,各個關(guān)鍵幀的時間是平分的 - CABasicAnimation可看做是只有2個關(guān)鍵幀的CAKeyframeAnimation
fillMode屬性值(要想fillMode有效,最好設(shè)置removedOnCompletion = NO)
kCAFillModeRemoved 這個是默認值,也就是說當(dāng)動畫開始前和動畫結(jié)束后,動畫對layer都沒有影響,動畫結(jié)束后,layer會恢復(fù)到之前的狀態(tài)
@interface NSObject (CAAnimationDelegate)/* Called when the animation begins its active duration. */- (void)animationDidStart:(CAAnimation *)anim;/* Called when the animation either completes its active duration or * is removed from the object it is attached to (i.e. the layer). 'flag' * is true if the animation reached the end of its active duration * without being removed. */- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;@end
#pragma mark 暫停CALayer的動畫-(void)pauseLayer:(CALayer*)layer{ CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; // 讓CALayer的時間停止走動 layer.speed = 0.0; // 讓CALayer的時間停留在pausedTime這個時刻 layer.timeOffset = pausedTime;}#pragma mark 恢復(fù)CALayer的動畫-(void)resumeLayer:(CALayer*)layer{ CFTimeInterval pausedTime = layer.timeOffset; // 1. 讓CALayer的時間繼續(xù)行走 layer.speed = 1.0; // 2. 取消上次記錄的停留時刻 layer.timeOffset = 0.0; // 3. 取消上次設(shè)置的時間 layer.beginTime = 0.0; // 4. 計算暫停的時間(這里也可以用CACurrentMediaTime()-pausedTime) CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; // 5. 設(shè)置相對于父坐標系的開始時間(往后退timeSincePause) layer.beginTime = timeSincePause;}
新聞熱點
疑難解答