




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
Cocoa系統(tǒng)豐富動畫效果,帶給用戶很多獨(dú)特的視覺和使用體驗(yàn)。CoreAnimation在UI系統(tǒng)架構(gòu)中處于關(guān)鍵位置,它是圖形渲染和動畫的基礎(chǔ),依賴底層的硬件加速特性,CoreAnimation可以獲得更能,CoreAnimation提供了多種不同的特性的圖層Layer滿足不同場景中使用動動畫產(chǎn)生過動畫的本質(zhì)是圖形對象在坐標(biāo)系統(tǒng)中基于時(shí)間參數(shù)變化,可以簡單的用數(shù)學(xué)公式描述如下Pn=Ft(p)其中Pn和p分別表示為圖形某個屬性變化后值和變化前的值,Ft為時(shí)間函數(shù)舉一個例子,要實(shí)現(xiàn)把小球從A點(diǎn)移動到B點(diǎn)的動畫,首先需要確定球的運(yùn)動軌跡,然后在軌跡曲線上確定一些關(guān)鍵坐標(biāo)點(diǎn)然后在不同時(shí)刻移動小球到關(guān)鍵位置,這樣就完成了一次動畫。CoreAnimation提供了幾種常見的動畫控制函數(shù)Linear線性:EaseIn淡入:EaseOut淡出:EaseInOut淡入淡出:開始慢,中間勻速,最后除了這些之外還可以通過CGPat,曲線靈活的定義動畫控制函數(shù)來實(shí)現(xiàn)各種動畫。使用CoreAnimation動畫框架,修改視圖層的屬性,自動使用默認(rèn)的控制函數(shù)產(chǎn)生動畫效果層Layer代表了3D空間中2D圖形的表面,它是CoreAnimation的,跟視圖有一些相同概念,括坐標(biāo)系統(tǒng),可視化的屬性等等Layer可以認(rèn)為是視圖bitmap的模型管理類,代表了圖形學(xué)中的2D對象iOS系統(tǒng)中,每個UIView系統(tǒng)都會默認(rèn)創(chuàng)建一個層Layer;而在OSX系統(tǒng)中,必須指定需要使用層即設(shè)置NSView對象的wantsLayer屬性為YES,而且它的所有子視圖也自動允許使用層屬性ViewA有2個子視圖ViewBViewC,在iOS每個視圖都默認(rèn)有一個對應(yīng)的layer層屬性,可OSX中設(shè)置了視圖允許使用層wantsLayer屬性為YES后,它的所有子視圖也默認(rèn)允許使用層在OSX中設(shè)置ViewA的wantsLayer屬性為YES以后,它的子視圖ViewB和ViewC就能直接使用視圖與Layr不同的是視圖支持各種用戶交互事件和屬性Layr僅僅是一個幾何圖形或圖象,不具備用戶交互特征和功能;視圖中的繪制在CP執(zhí)行CLayr繪制在GP執(zhí)行。支持aer的視圖稱為layer-baced的視圖,iOS中的視圖,SX中視圖設(shè)置wantaer為YS后,都是laer-baced的視圖。laer-baced的視圖有系統(tǒng)負(fù)責(zé)aer的管理創(chuàng)建,以及視圖和er之間內(nèi)容同步,比如修改了視圖的幾何屬性(大小,位置等),能同步到ae。除了layer-baced的視圖,SX中還可以創(chuàng)建基于lae-oig的視圖,ApKt不再管理這個ae,視圖跟layer-hoig的ayer同步需要自己編程處理。視圖是層的,實(shí)現(xiàn)了CALayerDelegate協(xié)議。層在渲染顯示時(shí),調(diào)用鏈關(guān)系依次如從上面的調(diào)用關(guān)系看出,通過NSView/UIViewdrawLayer:inContext:或drawRect:方法,可以實(shí)+(+(Class)layerClassreturn[CAMetalLayer}在OSX中可以使用下面的方法給視圖配置一個自定義的-(-(CALayer*)makeBackingLayerreturn[[CustomLayerclass]alloc]init}坐標(biāo)系般被分為基于點(diǎn)的坐標(biāo)系統(tǒng)和基于像素的坐標(biāo)系統(tǒng)。點(diǎn)的坐標(biāo)系統(tǒng)是一種邏輯標(biāo),跟設(shè)備無關(guān),一個點(diǎn)跟一個像素不是一一對應(yīng)的。在Layer層中,引入了一種新的單位標(biāo)系統(tǒng),每個坐標(biāo)軸值的范圍都是0到1之間,單位坐標(biāo)系統(tǒng)是針對aer的本身的Bouns來定義的,也相當(dāng)于對Bounds進(jìn)行了單位化歸一處理,achoroint用來規(guī)定aer的位置參考點(diǎn),ae層的position屬性是根據(jù)層的frame和Achoroint計(jì)算得出的anchorPointanchorPoint在iOS/OSX中單位坐標(biāo)定義如下下面是層在改變的AnchorPoint情況下position的變化Laer的旋轉(zhuǎn)變換是以crPt做為旋轉(zhuǎn)的參考軸來進(jìn)行的,因此對于同一個Layr不同的crPt坐標(biāo)會帶來不同的變換效果。層的坐標(biāo)系統(tǒng)轉(zhuǎn)CALayer提供了不同圖層之間坐標(biāo)轉(zhuǎn)換的方法,包括Point和Rect-(CGPoint)convertPoint:(CGPoint)pfromLayer:(CALayer-(CGPoint)convertPoint:(CGPoint)ptoLayer:(CALayer類似于坐標(biāo)系統(tǒng),不同層的系統(tǒng)時(shí)鐘也是本地化的,每個層使用本地時(shí)間系統(tǒng)來管理動畫。CALayer層提供了2CACurrentMediaTime函數(shù)可以得到計(jì)算機(jī)的全局時(shí)間,通過轉(zhuǎn)換獲取層的本地時(shí)間CFTimeIntervalCFTimeIntervallocalLayerTime=[myLayerconvertTime:CACurrentMediaTime()Layer定義了3種不同的對象樹,分別模型對象樹modellayer:用戶交互的對象的模型,Layer的各種屬性值,比如移動到某個位置點(diǎn),目標(biāo)點(diǎn)的坐標(biāo)就在對象樹中selfself.imageView.wantsLayer=CALayer*theLayer=self.imageView.layer;CALayer*modelLayer=theLayer.modelLayer;NSLog(@"modelLayeropacity%f展示對象樹presentationtree:動畫運(yùn)動過程中不同時(shí)刻展示的動態(tài)的屬性值,反映屏幕上CALayer*presentationLayer=theLayerCALayer*presentationLayer=theLayer.presentationLayer;NSLog(@"presentationLayeropacity%f",presentationLayer.opacity);渲染對象樹rendertree:動畫渲染時(shí)使用,系統(tǒng)私有對象,不提供對外接口類似于NSView/UIView,Layer提供了層級管理方法增加-(void)addSublayer:(CALayer層-(void)insertSublayer:(CALayer*)layer-(void)insertSublayer:(CALayer*)layerbelow:(nullableCALayer-(void)insertSublayer:(CALayer*)layerabove:(nullableCALayer替換-(void)replaceSublayer:(CALayer*)layerwith:(CALayer刪除-Layer提供@property(readonly)CALayer*superlayer;@property(copy)NSArray*sublayers;Layer有contents內(nèi)容屬性,可以是來自Layer的繪制方法,或者來自一個指定的,總共有3類Layer的內(nèi)容來自UIImageUIImage*image=[UIImageimageNamed:@"gradintCell"];blueLayer.contents=(bridgeid)image.CGImage;OSX系統(tǒng)實(shí)NSImageNSImage*image=[NSImageimageNamed:@"gradintCell"];blueLayer.contents=image;通過Layer方有2個方法,只需要實(shí)現(xiàn)其中一種即可,2種方法都實(shí)現(xiàn)時(shí),只會調(diào)用第一種NSImage*image=[NSImageimageNamed:@"gradintCell"];theLayer.contents=image;}CGContextSetFillColorWithColor(theContext,[NSColorblueColor].CGColor);CGContextFillRect(theContext,theLayer.bounds);}子類化通過子類化CALayer,提供Contents內(nèi)容。只需要實(shí)現(xiàn)下面2種任意法。display方法要直接提供圖象給contents,drawInContext方法可以通過繪圖函數(shù)自己實(shí)現(xiàn)自定義圖形繪制NSImageNSImage*image=[NSImageimageNamed:@"gradintCell"];self.contents=image;}{CGContextSetLineWidth(ctx,4.0f);CGContextSetFillColorWithColor(ctx,[NSColorblueColor].CGColor);CGContextFillRect(ctx,self.bounds);}層的深度層的zPosition決定了顯示的順序,zPosition最大的層顯示在最外層CALayer*parentLayer=myView.layer;CALayerCALayer*parentLayer=myView.layer;CALayer*blueLayer=[CALayerlayer];blueLayer.frame=NSMakeRect(10,10,100,100);blueLayer.backgroundColor=[NSColorblueColor].CGColor;[parentLayeraddSublayer:blueLayer];CALayer*greenLayer=[CALayerlayer];greenLayer.frame=NSMakeRect(20,20,100,100);greenLayer.backgroundColor=[NSColorgreenColor].CGColor;[parentLayeraddSublayer:greenLayer];CALayer*redLayer=[CALayerlayer];redLayer.frame=NSMakeRect(30,30,100,100);redLayer.backgroundColor=[NSColorredColor].CGColor;[parentLayeraddSublayer:redLayer];如果修改greenLayer的zPosition為500,修改前后顯層的投影使用subLayerTransform變換來產(chǎn)生投影,設(shè)置3D變換矩陣中的m34字段即可CALayer*parentLayer=myView.layer; =CATransform3DIdentity;.m34=-parentLayer.sublayerTransform CALayer*blueLayer=[CALayerlayer];blueLayer.frame=NSMakeRect(10,10,100,100);blueLayer.backgroundColor=[NSColorblueColor].CGColor;blueLayer.zPosition=50;[parentLayerCALayer*greenLayer=[CALayerlayer];greenLayer.frame=NSMakeRect(20,20,100,100);greenLayer.backgroundColor=[NSColorgreenColor].CGColor;greenLayer.zPosition=80;[parentLayerCALayer*redLayer=[CALayerlayer];redLayer.frame=NSMakeRect(30,30,100,100);redLayer.backgroundColor=[NSColorredColor].CGColor;redLayer.zPosition=200;[parentLayerLayer動隱式動畫:無需配置系統(tǒng)按默認(rèn)參數(shù)實(shí)現(xiàn)的動畫(動畫持續(xù)時(shí)間為2秒)。對ayer(frmVae,修改后為tVae會產(chǎn)生frmVaeVae平滑過的動畫屬性的值會立即更新到meLaer模型樹對象而展示樹resetatLayer。只有新創(chuàng)建的Layer修改屬性會產(chǎn)生隱式動畫。UIView/NSView每個視圖對應(yīng)的層,layer-backed層,修它的屬性并不能產(chǎn)生隱式動畫下面是一個隱式動畫的例子:創(chuàng)建一個背景層gLyermVe的ayr上做為子層。點(diǎn)擊按鈕使背景層gLayer。iOS{[superCALayer*bgLayer=[CALayerlayer];bgLayer.frame=CGRectMake(10,10,50,50);bgLayer.backgroundColor=[UIColorredColor].CGColor;[self.myView.layeraddSublayer:bgLayer];self.bgLayer=bgLayer;}- selfself.bgLayer.opacity=}OSX中隱式{[superCALayer*bgLayer=[CALayerlayer];bgLayer.frame=CGRectMake(10,10,50,50);bgLayer.backgroundColorUIColorredColor].CGColor;self.myView.wantsLayer=YES;//要求myView使用層[self.myView.layeraddSublayer:bgLayer];self.bgLayer=}{self.bgLayer.opacity=}顯式動畫:通過[CALayeraddAnimation:forKey:]顯式的增加動畫(設(shè)置屬性變化值,動畫持續(xù)時(shí)CABasicAnimationCABasicAnimation*animation=[CABasicAnimationanimation];animation.fromValue=...;animation.toValue=...anchorPoint:contents:內(nèi)contentsRect:內(nèi)容opacity:不shadowOpacity:陰影不 zPosition:比如transform支持的子屬性可以使用setValue:forKeyPath:方法直接修層屬性層動畫屬性的修改,不支持簡單的值數(shù)據(jù)或結(jié)構(gòu)體數(shù)據(jù),需要使用值類型對象進(jìn)行包裝對空間數(shù)據(jù)結(jié)構(gòu)需要使用值類型CGPointNSValueCGSizeNSValueCGRectNSValueCATransform3DNSValueCGAf?neTransformNSAf?neTransform(OSX下面是一個實(shí)現(xiàn)視圖位置移動的顯式動畫的例子iOS實(shí)CGPointCGPointmyNewPosition=CGPointMake(100,CABasicAnimation*theAnim=[CABasicAnimationanimationWithKeyPath:@"potheAnim.fromValue=[NSValuevalueWithCGPoint:self.myView.layer.positio =[NSValuevalueWithCGPoint:myNewPosition]; =3.0;OSX實(shí)現(xiàn):有3通過修個層的position屬NSPointNSPointmyNewPosition=CABasicAnimation*theAnim=[CABasicAnimationanimationWithKeyPath:@"potheAnim.fromValue=[NSValuevalueWithPoint:self.myView.layer.position]theAnim.toValue=[NSValuevalueWithPoint:myNewPosition];theAnim.duration=3.0;上面代碼并不能產(chǎn)生動畫效果,需要將myView的父視圖設(shè)置為允許使用層如下{[self.window.contentView}通過層在3D空間XY軸平CABasicAnimationCABasicAnimation*animation=[CABasicAnimationanimationWithKeyPath:@"transanimation.duration=2.0; animation.repeatCount=animation.fromValue=[NSValue addAnimation:animation上述方法同樣的需要設(shè)置myView的父視圖為允許使用層通過NSViewAnimation定義動NSRectNSRectstartFrame=[self.myViewNSRectendFrame=NSMakeRect(100,100,startFrame.size.width,startFrame.size.hNSDictionary*dictionary=[[NSDictionaryalloc]initWithObjectsAndKeys: [NSValueNSViewAnimation*animation=[[NSViewAnimationalloc]initWithViewAnimations:[Nanimation.duration=2;[animationsetAnimationCurve:NSAnimationEaseIn];[animation動畫對可以分為動畫/協(xié)議/工具/事務(wù)4大類動畫基礎(chǔ)類,定義了通用的屬性和方法@interface@interfaceCAAnimation:+(id)defaultValueForKey:(NSString//動畫效果控制函數(shù)動畫協(xié)定義了動畫開始執(zhí)行和結(jié)束后的回調(diào)接口@interface@interfaceNSObject-(void)animationDidStart:(CAAnimation屬性動畫即基于某個屬性來產(chǎn)生動畫。這里的屬性要特別說明一下的實(shí)際屬性這些屬性只有要進(jìn)行動畫的時(shí)候才能使用稱之為虛擬屬性(比如trafmrtatn不能做為Laer屬性直接設(shè)置)。@interface@interfaceCAPropertyAnimation://創(chuàng)建屬性動畫,path規(guī)定產(chǎn)生動畫的屬性 //動畫屬性對應(yīng)的@property(copy)NSString//additive為動畫規(guī)定的values值在當(dāng)前值基礎(chǔ)上疊加做為新的值去渲染,也就是說values中規(guī)定@property(getter=isCumulative)BOOLcumulative;//tranform變換對應(yīng)的函數(shù)最基本的值變化動畫,指定初始值fromValue和結(jié)束值toValue,系統(tǒng)使用默認(rèn)的動畫效果動畫byValue不是必須的,可用來規(guī)定變化的步長,fromValue加上byValue做為下一個新值frmVae和tVae不一定要同時(shí)設(shè)定如果mVae為空則表示產(chǎn)生從當(dāng)前值到tVa的。如果toValue為空則表示產(chǎn)生從fromValue到當(dāng)前值的動畫@interface@interfaceCABasicAnimation://@property(strong)id//@property(strong)id關(guān)鍵幀動畫,分為基于關(guān)鍵幀數(shù)據(jù)的動畫和基于路徑的動畫2種@interfaceCAKeyframeAnimation://@property(copy)NSArray@property(copy)NSArray<NSNumber*>*keyTimes;//時(shí)間函數(shù),用來指定values中幀數(shù)值之間的過渡的變化函數(shù),其元素個數(shù)必須為values中元素個數(shù)減1。@property(copy)NSArray<CAMediaTimingFunction*>*timingFunctions;//@property(copy)NSString//Cubic曲線對應(yīng)的控制參數(shù)@property(copy)NSArray*tensionValues;@property(copy)NSArray*continuityValues;@property(copy)NSArray*biasValues;//旋轉(zhuǎn)模式,設(shè)為非空值可以沿著路徑方向旋轉(zhuǎn),//計(jì)算模式(插值算法NSString*constkCAAnimationDiscrete//離散點(diǎn),只顯示關(guān)鍵幀的狀態(tài),沒有中間過程和動畫。NSString*constkCAAnimationPaced//平滑的速度NSString*constkCAAnimationCubic//圓滑曲線NSString*constkCAAnimationCubicPaced//圓滑加均勻基本動畫和關(guān)鍵幀動畫每次只能對一個屬性進(jìn)行動畫,而組動畫可以將任意的單一屬性動畫組組,同時(shí)在多個屬性產(chǎn)生動畫@interface@interfaceCAAnimationGroup:CAAnimation@property(copy)NSArray<CAAnimation*>*animations;轉(zhuǎn)場動畫不是對層屬性來做動畫而是特指2個不同的層即場景切換時(shí)產(chǎn)生的動畫。一張切換到另外一張個視圖之間的切換等等。@interface@interfaceCATransition:CAAnimation@property(copy)NSString*type;//動畫類型@propertycopyNSString*subtype;//子類型@propertyfloatstartProgress;//開始時(shí)間百分比系數(shù),默認(rèn)為0@propertyfloatendProgress;//結(jié)束時(shí)間百分比系數(shù),默認(rèn)為1@property(strong)id//轉(zhuǎn)場變換類型NSString*constkCATransitionFade//淡入NSString*constkCATransitionMoveIn//淡出NSString*constkCATransitionPush//壓入NSString*constkCATransitionReveal//融入//NSStringNSString*constkCATransitionFromRight//從右側(cè)NSString*constkCATransitionFromLeft//從左側(cè)NSString*constkCATransitionFromTop//從頂側(cè)NSString*constkCATransitionFromBottom//從頂部類似彈簧的阻尼@interface@interfaceCASpringAnimation:@propertyCGFloatmass;@propertyCGFloatstiffness;@propertyCGFloatdam //@propertyCGFloat//延時(shí)2CFTimeIntervalCFTimeIntervallocalLayerTime=[myLayerconvertTime:CACurrentMediaTime()theAnimation.beginTime=localLayerTime+2;theAnimation.duration=5.0;動畫時(shí)間5秒,沒有延時(shí)與延時(shí)2秒的對timeOffset(動畫幀的偏移量CFTimeIntervalCFTimeIntervallocalLayerTime=[myLayerconvertTime:CACurrentMediaTime()theAnimation.timeOffset=localLayerTime+2;theAnimation.duration=5.0;動畫時(shí)間5秒,沒有timeOffset與timeOffset2秒后的對比speed(動畫速度動畫速度影響動畫的持續(xù)時(shí)間,速度越大,動畫時(shí)間越短,速度默認(rèn)是。動畫持續(xù)時(shí)間計(jì)算公式如下duration=duration/repeatCount(重復(fù)次數(shù))repeatDuration(重復(fù)的時(shí)間repeatCount和repeatDuration只需要按需要設(shè)置其一。同時(shí)設(shè)置時(shí)取時(shí)間最短的一個autoreverses自動反動畫完成后在按原路返回。比如原本秒的動畫幀序列為C設(shè)置atrevers為S后執(zhí)行結(jié)果為CCBA。?llModekCAFillModeRemoved:默認(rèn)無填充kCAFillModeForwards:動畫結(jié)束后維持最后一個時(shí)刻的狀態(tài)。例如5秒的動畫 結(jié)束后維持在狀態(tài)。結(jié)合設(shè)置動畫的 kCAFillModeBackwards:當(dāng)動畫的beginTime不為0時(shí),動畫創(chuàng)建完成,未開始之前的時(shí)間內(nèi)一直延時(shí)2秒的動畫kCAFillModeBackwards填充模式示kCAFillModeBoth:是kCAFillModeBackwards和kCAFillModeForwards2種情況的結(jié)合動畫動作協(xié)議從動畫對象圖譜中可以看出動畫的基類Cmat實(shí)現(xiàn)了Cct協(xié)。修改單獨(dú)的層屬性觸發(fā)隱式動畫隱式動畫查找每個屬性對應(yīng)的實(shí)現(xiàn)了Cct協(xié)議的動畫對象(即缺省的動畫類大多數(shù)情況下為Cacmat),動畫對象執(zhí)行Cct協(xié)議runActionForKey:object:arguments方法,將自己加入系統(tǒng)動畫隊(duì)列等待執(zhí)行@protocol@protocol-(void)runActionForKey:(NSString*)eventobject:(id)anObjectarguments:(nullableNSDictionary*)dict;隱式layer.opacity=借助于CAAction協(xié)議,相當(dāng)于CABasicAnimationCABasicAnimation*animation=[CABasicAnimationanimation];animation.toValue=@.5;@interface@interfaceCAMediaTimingFunction:NSObject// 曲+(instancetype)functionWithControlPoints:(float)c1x:(float)c1y:(float)c2x- //NSString*constkCAMediaTimingFunctionLinearNSString*constkCAMediaTimingFunctionEaseInNSString*constkCAMediaTimingFunctionEaseOutNSString*constkCAMediaTimingFunctionEaseInEaseOutNSString*const@interface@interfaceCAValueFunction:NSObject@property(readonly)NSString*name;NSString*constkCAValueFunctionRotateXNSString*constkCAValueFunctionRotateYNSString*constkCAValueFunctionRotateZNSString*constkCAValueFunctionScaleNSString*constkCAValueFunctionScaleXNSString*constkCAValueFunctionScaleYNSString*constNSString*constkCAValueFunctionTranslateNSString*constkCAValueFunctionTranslateXNSString*constkCAValueFunctionTranslateYNSString*const 成對使用定義動畫可以嵌套使用必須保證 的隱式動畫實(shí)際上是使用CATransaction的方式定義后提交到任務(wù)調(diào)度的runLoop等待執(zhí)行使用Cractn的方式可以更靈活的指定動畫的時(shí)間完成后的ck回調(diào)指定動畫效果的。@interface@interfaceCATransaction:++//動畫持續(xù)的時(shí)間,默認(rèn)為0.25++//動畫時(shí)間變換函數(shù),默認(rèn)為+(nullableCAMediaTimingFunction//動畫默認(rèn)的action開關(guān),初始值為++////動畫完成+(void+ pletionBlock:(nullablevoidCATransaction支持KVC形式的setValue:forKey:設(shè)置和屬下面是系統(tǒng)預(yù)定義的個key,分別對應(yīng)Crctn類中定義的個接口方法。除了系統(tǒng)的定義的之外可以定義自己的key來傳入?yún)?shù),在整個CAraction上下文中。NSString*constkCATransactionAnimationDurationNSString*constkCATransactionDisableActionsNSString*constkCATransactionAnimationTimingFunctionNSString*const 下面是使用CATransaction[CATransactionsetAnimationDuration:1];關(guān)閉隱式動畫的self.bgLayer.opacity=0.2;動畫的動作控 1.1.查找實(shí)現(xiàn)了CAAction協(xié)議修改一個層的屬性,立即觸發(fā)它的actionForKey:方法去查找CAAction類型對象,查找流程依次優(yōu)先檢測層是否有elgae并且實(shí)現(xiàn)了acioFoaer:foy:方法,如果有返回AAction類型對象,則返回這個對象;如果返回il則繼續(xù)分支流程處理;如果返回NSNl則結(jié)束后續(xù)搜索分支通過key查找層的actions字典,如果查找成功則返回CAAction類型對象,否則繼續(xù)下一步去查查找層的yle字典中的y為action的字典,如果不為空則在新的action字典中繼續(xù)用y去查找執(zhí)行層的defaultActionForKey:方法2.執(zhí)行CAAction協(xié)議,創(chuàng)建一個對搜索到的動畫對象,執(zhí)行Cctn協(xié)議方法rctFrKey:ectagmet:,這個方法的第一個參數(shù)key為一個CLayer屬性名稱,第二個參數(shù)oect為CLayer層對象。執(zhí)行的過程是在CLyer層上根據(jù)屬性參數(shù)修改前和修改后的值,通過層的amatfrKe:方法,創(chuàng)建一個顯式動畫。對于正常的NSView/UIView的layer的屬性修改,并不產(chǎn)生隱式動畫的原因在于執(zhí)行上述隱式根據(jù)CLaer層中查找Cct動畫對象的執(zhí)行過程可以根據(jù)屬性參數(shù)進(jìn)行返回指定的動畫對象來影響只求默認(rèn)返回的動畫。修改CALayer中actionForKey:方bounds變化的動{if([eventisEqualToString:@"bounds"])returnnil;return[super}2.2.修改視圖NSView/UIView中實(shí)現(xiàn)的actionForLayer:forKey:層的內(nèi)容變化時(shí),實(shí)現(xiàn)一個自定義轉(zhuǎn)場-(id-(id<CAAction>)actionForLayer:(CALayer*)layerforKey:(NSString{if([keyisEqualToString:@"contents"])CATransition*theAnimation=[[CATransitionalloc]init];theAnimation.duration=1.0; =kCATransitionPush;returntheAnimation;}return[superactionForLayer:layerforKey:key]}動畫執(zhí)行完成后,可以使用方法或動畫事務(wù)完成塊Block得到回調(diào)通知CABasicAnimation*animation1=[CABasicAnimationanimation];animation1.keyPath=@"borderWidth";animation1.toValue=@(10);animation1.delegate=}{ =}動畫事務(wù)完成Block回CABasicAnimation*animation1=[CABasicAnimationanimation];animation1.keyPath=@"borderWidth";animation1.toValue=animation1.delegateanimation1.delegate=[CATransactioncommit]; =暫停-(-(void)pauseLayer:(CALayer*)layerCFTimeIntervalpausedTime=[layerconvertTime:CACurrentMediaTime()layer.speed=0.0;}恢復(fù)-(-(void)resumeLayer:(CALayer*)layer{CFTimeIntervalpausedTime=[layertimeOffset];layer.speed=1.0;layer.timeOffset=layer.beginTime=CFTimeIntervaltimeSincePause=[layerconvertTime:CACurrentMediaTime()fromLayer:nil]-pausedTime;layer.beginTime=}OSX中的視圖動NSView/NSWindow實(shí)現(xiàn)了NSAnimatablePropertyContainer,當(dāng)視圖的的變化時(shí),可以產(chǎn)生動畫效果;通過animator可以透明的使用這些屬性來產(chǎn)生動畫動畫協(xié)@protocol@protocol--(void)setAnimations:(NSDictionary-(id)animationForKey:(NSString*)key;修改視圖屬性產(chǎn)self.myView.animator.frame=自定義屬性添加@interface@interfaceCustomView:NSView@property(nonatomic,strong){_lineColor=[NSColor}[_lineColorset];}-(void)setLineColor:(NSColor*)lineColor}修改defaultAnimationForKey方法,給自定義屬性lineColor配置缺省{if([keyisEqualToString:@"lineColor"]){return[CABasicAnimationanimation];}}修改lineColor屬性產(chǎn)生動self.myView.animator.lineColor=[NSColor修改視圖的animations中了屬性關(guān)聯(lián)的動畫,修改屬性時(shí),優(yōu)先從這個字典獲取屬性對應(yīng)的動畫CAKeyframeAnimationCAKeyframeAnimation*kfa=[CAKeyframeAnimationkfa.values=@[[NSColorgreen],[NSColorredColor],[NSColoryellowColor]];view.animations=@{@"lineColor":kfa};view.animator.lineColor=[NSColor通過NSmatCtext動畫上下文對象可以對動畫的時(shí)長時(shí)間函數(shù)是否允許隱式動畫等做全局控制。;;@propertyNSTimeInterval@property(nullable,strong)CAMediaTimingFunction*timingFunction;@property(nullable,copy)void(^completionHandler)(void);@propertyBOOLallowsImplicitAnimation;鏈?zhǔn)絼赢?多個動畫依次 .animator.frame=}completionHandler:^{[NSAnimationContextrunAnimationGroup:^(NSAnimationself.view2.animator.frame=控制修改NSAnimationonxt的allwmlicitimaion為YS后則直接修改視圖的屬性就會產(chǎn)生動畫僅支持frame,frameSize,frameOrigin3個屬性;的屬性動畫需要layer-backed的視圖,即視通過的animator修改frame執(zhí)行動view.animator.frameview.animator.frame=可以簡化為直接修改frame屬view.frame=toValue;的屬性,通過修改層的背景色產(chǎn)生//允許使用層self.myView.wantsLayerYES;AutoLayout增加Aoaot約束后,通過在NSAnimationCoxt的runAnimationBlock方法中執(zhí)行視圖或窗口的更新方法實(shí)現(xiàn)隱式動畫。[[self.view*context){context.allowsImplicitAnimation=YES;[self.viewlayoutSubtreeIfNeeded];/*/*OR[windowAppKitAnimationVSCoreAppKit動動畫做為視圖的屬性,運(yùn)行在主線程;drawRect繪制周期修改視圖的屬性Core動畫作為CALayer的屬性,運(yùn)行在線程;只修改渲染層的屬性下面是視圖size變化時(shí)動畫過程,CoreAnimation只有在開始的t1和最后的t4時(shí)刻工作在主線程,其他時(shí)刻均在線程運(yùn)行;動畫編程示關(guān)鍵幀動畫分為基于關(guān)鍵幀數(shù)據(jù)和基于路徑的兩種形式基于文本輸入框,用戶輸入出錯后,使用關(guān)鍵幀動畫實(shí)現(xiàn)左右勻速晃動的動畫CAKeyframeAnimationCAKeyframeAnimation*animation=[CAKeyframeAnimationanimationWithKeyPath:animation.additive=YES;animation.values=@[@(-15),@(15),@(-10),@(10),@(-8),@(8),@(-6),@(6),@(-4)animation.duration=1;[self.userNameTextField.layeraddAnimation:animation基于實(shí)現(xiàn)一個層沿著圓形軌道旋轉(zhuǎn)的動畫首先定義一個圓形圖層,這個圓形是通過一個CSaeLyr層做為mk來實(shí)現(xiàn)。定義需要旋轉(zhuǎn)的圖層,添加關(guān)鍵幀動畫,關(guān)鍵幀的ath定義為一個圓形,旋轉(zhuǎn)模式為rtatioMoe自動。NSViewNSView*view=[self.windowcontentView];CALayer*rootLayer=view.layer;CAShapeLayer*circleShapeLayer=[CAShapeLayerlayer];CGMutablePathRefshapePath=CGPathCreateMutable();CGPathAddEllipseInRect(shapePath,nil,CGRectMake(0,0,160,160));circleShapeLayer.path=shapePath;//CALayer*circleLayer=[CALayerlayer];circleLayer.frame=CGRectMake(60,60,160,160);circleLayer.backgroundColor=[NSColorpurpleColor].CGColor;circleLayer.mask=circleShapeLayer;circleLayer.masksToBounds=YES;[rootLayer//CALayer*colorLayer=[CALayerlayer];colorLayer.frame=CGRectMake(-10,-10,20,20);colorLayer.backgroundColor=[NSColorgreenColor].CGColor;[rootLayeraddSublayer:colorLayer];//CAKeyframeAnimation*animation1=[CAKeyframeAnimationanimation]; =@"position"; =CGPathCreateWit animation1.rotationMode=kCAAnimationRotateAuto;animation1.repeatCount=100000; =YES; =2.0;animation1.calculationMode=kCAAnimationPaced;將多個動畫組一組來同時(shí)運(yùn)行CALayer*layer=[CALayerlayer.frame=CGRectMake(10,10,100,100);[rootLayeraddSublayer:layer];CABasicAnimation*animation1=[CABasicAnimationanimation];animation1.keyPath=@"borderWidth";animation1.duration=4.0;CABasicAnimation*animation2=[CABasicAnimationanimation];animation2.keyPath=@"backgroundColor";animation2.toValue=(bridgeid)[NSColorCAAnimationGroup*groupAnimation=[CAAnimationGroupanimation];groupAnimation.animations=@[animation1,animation2];groupAnimation.duration=4.0;// pletion=//保持動畫完成后的幀狀態(tài)[layeraddAnimation:animation1forKey:nil];層的內(nèi)容發(fā)生變化或視圖的隱藏/顯示,這些場景變化時(shí)可用使用ransition產(chǎn)生一種過渡動畫,可實(shí)現(xiàn)推入、移動、淡入淡出動畫效果;另外OSX可以使用CFler自定義動畫變化效果。圖象輪播組件中magVe的圖象的LaerCratn轉(zhuǎn)場動畫來實(shí)現(xiàn)圖象切換的動畫效果。-(-(void)transitionAnimationCATransition*transition=[CATransitionanimation];transition.startProgress=0.5;transition.endProgress=1.0;transition.type=kCATransitionPush;transition.duration=}{[selftransitionAnimation];}動畫性減少在SX中的視圖設(shè)置了wantayer=YS后所有子視圖都有了ayer層,如果圖層較多會帶來了性能問題:每個層有自己的圖像區(qū),的層產(chǎn)生了額外的內(nèi)存開多個層圖象的開隱藏的圖層也會產(chǎn)生的代在設(shè)置了視圖的wantae=YS后,通過設(shè)置視圖的canDrawSuviewsInoaer=YS,這樣每個子視圖就不會使用aer層。如果某個子視圖需要使用層,可以單獨(dú)自己設(shè)置允許使用層。盡可能避免操作@propertyCGFloatcornerRadius;@property(retain)CALayer*mask;@property(copy)NSArray*?lters;@property(copy)NSArray盡可能設(shè)置視圖的非透明屬性使用Updaayers優(yōu)化繪傳統(tǒng)的視圖繪制是基于CPU的drawRect:方式,如果視圖的wantsUpdaayer方法返回YES,就不在調(diào)用drawRect:方法,而執(zhí)行updaayer方法,在這個方法中直接修改層的內(nèi)容來實(shí)現(xiàn)視圖 {return} ayerself.layer.borderColor=NSColor.redColor.CGColor;}}系統(tǒng)專有CLaer做為通用的層對象位置大小旋轉(zhuǎn)等基本的動畫支持。提供了很多專門的Layr。使用硬件加速實(shí)現(xiàn)一個漸變層,比CoreGraphics中使用NSGradient性能高。startPoint和=[CAGradientLayer =CGRectMake(0,0,200,gradientLayer.colors=@[(id)[NSColor(id)[NSColorredColor].CGColor,(id)[NSColorcyanColor].CGColor,(id)[NSColorblueColor].CGColor];gradientLayer.startPoint=CGPointMake(0,0);gradientLayer.endPoint=CGPointMake(1,1);使用Path定義的幾何圖形或者任意的曲線的矢量圖,做為CAShapeLayer的Path,實(shí)GPU加速繪制除了基本的矢量圖繪制外,CAShapeLayer還可以實(shí)現(xiàn)圖形的動畫繪制CSaeLyer不能對區(qū)域填充實(shí)現(xiàn)動畫。keStart了起點(diǎn)點(diǎn)ke定義了結(jié)束點(diǎn)(值的范圍是按百分比定義的都是到之間的浮點(diǎn)數(shù)ke值要大于kekCr定義了畫筆的顏色。CSaeLayerkeStrt到ke之間繪制Pat路徑時(shí)產(chǎn)生動畫。直線段繪制實(shí)現(xiàn)進(jìn)度條動畫的一個例子定義了CAShapeLayer層對象和直線段path,設(shè)置strokeStart和strokeEnd初始值。timer每隔1秒修改路徑的strokeEnd值,產(chǎn)生隱式動畫-{[superCAShapeLayer*shapeLayer=[CAShapeLayerlayer]; =CGRectMake(0,0,200,CGMutablePathRefpath=CGPathCreateMutable();CGPathMoveToPoint(path,NULL,0,20);CGPathAddLineToPoint(path,NULL,200,shapeLayer.path=path;shapeLayer.strokeEnd=shapeLayer.strokeColor=[NSColorgreenColor].CGColor;[self.layerView.layeraddSublayer:shapeLayer];self.timer=[NSTimerscheduledTimerWithTimeInterval:1 :selfselector[[NSRunLoopcurrentRunLoop]addTimer:self.timer }{if(self.shapeLayer.strokeEnd<1.0){self.shapeLayer.strokeEnd+=0.1;}}}方便處理文本的層,支持NSString或NSAttributedString的格式文本CATextLayerCATextLayer*textLayer=[[CATextLayeralloc]init];[textLayersetFontSize:14.0];[textLayersetContentsScale:[[NSScreenmainScreen]backingScaleFactor]];[textLayersetFrame:CGRectMake(0,0,80,50)];[textLayersetString:@"textstring"];[textLayersetBackgroundColor:[[NSColorclearColor]CGColor]];[textLayersetForegroundColor:[[NSColorblackColor]CGColor]][self.leftView.layeraddSublayer:textLayer];一種分片顯示內(nèi)容的層,可以理解為把層劃分為小的網(wǎng)格,每個格子內(nèi)容單獨(dú)加載顯示顯示大的圖像或大的視圖。定義一個子類化視圖TileView,在makeBackingLayer方法中定義TileViewCATiledLayer類型在層方法raLaye:Ctet:中計(jì)算個格子的行r和列c分別處理。這個例子中是簡單隨機(jī)生成Cr繪制一個矩形。#import#import<Cocoa/Cocoa.h>@interfaceTileView:NSView#import@implementationTileView-(CALayer*)makeBackingLayerCATiledLayer*tileLayer=[CATiledLayerlayer];tileLayer.tileSize=CGSizeMake(60,60);tileLayer.delegate=self;return}{CGRectbounds=NSIntegerrow=floor(bounds.origin.x/layer.tileSize.width);NSIntegercol=floor(bounds.origin.y/layer.tileSize.height);}}CGContextFillRect(ctx,bounds);NSLog(@"row=%ldcol=%ld將TileView做為scrollView的文檔視TileViewTileView*layerView=[[TileViewalloc]initWithFrame:CGRectMake(0,0,360,self.scrollView.autohidesScrollers=YES; View=實(shí)現(xiàn)子層的多份,包括子層的動畫效果CAReplicatorLayerCAReplicatorLayer*copiedLayer=[CAReplicatorLayerlayer];[view.layeraddSublayer:copiedLayer]; 時(shí)延時(shí)間隔copiedLayer.instanceDelay=//顏色的RGB =-copiedLayer.instanceBlueOffset=- 的層之間X,Y,ZcopiedLayer.instanceTransform=CATransform3DMakeTranslation(20,0,0)CALayer*layer=[CALayerlayer.frame=CGRectMake(10.0f,10.0f,10.0f,100.0f);layer.backgroundColor=[NSColorCABasicAnimation*animation=[CABasicAnimation//位置Y坐標(biāo) =@(layer.position.y-arc4random()%100); =1.0f; =true; =層[copiedLayer粒子系統(tǒng)包括粒子發(fā)射源控制系統(tǒng)CAEmitterLayer和粒子模型CAEmitterCellCmtteLyer發(fā)射源申明全局的控制參數(shù)包括發(fā)射源的位置emttrPtemtterSz大小rtate每秒產(chǎn)生率reerMe渲染模式emtteSae發(fā)射源形狀初速度vecy。CAEmitterCell粒子模型,主要包括粒子內(nèi)容contents(CGImageRef類型),粒子的顏色color,速度velocity,縮放scale,方向旋轉(zhuǎn)spin,生命周期時(shí)間lifetime,xyz三個方向的加速度AccelerationCALayer*rootLayer=[myView//粒子源系統(tǒng)定義CAEmitterLayer*snowEmitter=[CAEmitterLayersnowEmitter.drawsAsynchronouslyYES;[snowEmittersetName:@"snowEmitter"];snowEmitter.zPosition=10.00f;snowEmitter.emitterPosition=CGPointMake(300.00f,300.00f);snowEmitter.renderMode=kCAEmitterLayerBackToFront;snowEmitter.emitterShape=kCAEmitterLayerCircle;snowEmitter.emitterZPosition=-43.00f;snowEmitter.emitterSize=CGSizeMake(160,160);snowEmitter.velocity=20.57f;snowEmitter.birthRate=10;//第一個粒子模型定義CAEmitterCell*snowFlakesCell=[CAEmitterCellemitterCell];[snowFlakesCellsetName:@"snowFlakesCell"];//粒子內(nèi)容圖象,必須是CGImageRefsnowFlakesCell.contents=(bridgeid)[[NSImageimageNamed:@"Ball_red.png"CGColorRefcolorRefSnowflakescell=CGColorCreateGenericRGB(0.77f,0.65f,0snowFlakesCell.color=colorRefSnowflakescell;snowFlakesCell.redRange=0.90f;snowFlakesCell.greenRange=0.80f;snowFlakesCell.blueRange=0.70f;snowFlakesCell.alphaRange=0.80f;snowFlakesCell.redSpeed=0.92f;snowFlakesCell.greenSpeed=0.84f;snowFlakesCell.blueSpeed=0.74f;snowFlakesCell.alphaSpeed=0.55f;snowFlakesCell.scale=0.72f;snowFlakesCell.spin=0.38f;snowFlakesCell.spinRange=0;snowFlakesCell
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 IEC TR 61850-7-6:2024 EN Communication networks and systems for power utility automation - Part 7-6: Guideline for definition of Basic Application Profiles (BAPs) using IEC
- 2025-2030年中國鍍鋅層鈍化劑行業(yè)發(fā)展趨勢及投資戰(zhàn)略研究報(bào)告
- 2025-2030年中國鉛酸蓄電池行業(yè)市場現(xiàn)狀分析規(guī)劃研究報(bào)告
- 2025-2030年中國針織服裝市場市場運(yùn)行動態(tài)及投資戰(zhàn)略研究報(bào)告
- 2025-2030年中國酮洛芬腸溶膠囊行業(yè)十三五規(guī)劃與發(fā)展趨勢分析報(bào)告
- 2025-2030年中國艾灸養(yǎng)生儀產(chǎn)業(yè)發(fā)展現(xiàn)狀及前景趨勢分析報(bào)告
- 2025-2030年中國美甲行業(yè)運(yùn)行現(xiàn)狀及發(fā)展前景分析報(bào)告
- 2025年四川省建筑安全員C證考試(專職安全員)題庫及答案
- 皖北衛(wèi)生職業(yè)學(xué)院《時(shí)間序列分析》2023-2024學(xué)年第二學(xué)期期末試卷
- 中央財(cái)經(jīng)大學(xué)《商務(wù)智能》2023-2024學(xué)年第二學(xué)期期末試卷
- 幼兒看圖填數(shù)
- 酒店項(xiàng)目精裝修工程施工組織設(shè)計(jì)
- 小學(xué)生研學(xué)旅行展示ppt模板
- 《思想道德與法治》第一章
- 新概念英語第2冊課文word版
- 大學(xué)生職業(yè)生涯規(guī)劃(高職)PPT完整全套教學(xué)課件
- 微信小程序開發(fā)實(shí)戰(zhàn)(第2版)全套PPT完整教學(xué)課件
- 部編版語文四年級下冊全冊大單元整體作業(yè)設(shè)計(jì)
- 重慶自然博物館
- 收養(yǎng)人撫養(yǎng)教育被收養(yǎng)人能力的證明
- 施工升降機(jī)的安裝步驟
評論
0/150
提交評論