iOS開發CABasicAnimation動畫理解

2022-07-24 20:18:18 字數 4884 閱讀 9881

1、calayer簡介

calayer是個與uiview很類似的概念,同樣有backgroundcolor、frame等相似的屬性,我們可以將uiview看做一種特殊的calayer。但實際上uiview是對calayer封裝,在calayer的基礎上再新增互動功能。uiview的顯示必須依賴於calayer。

我們同樣可以跟新建view一樣新建乙個layer,然後新增到某個已有的layer上,同樣可以對layer調整大小、位置、透明度等。

一般來說,layer可以有兩種用途:一是對view相關屬性的設定,包括圓角、陰影、邊框等引數;二是實現對view的動畫操控。因此對乙個view進行動畫,本質上是對該view的.layer進行動畫操縱。

2.caanimation的分類

(1) cabasicanimation

基礎動畫,通過設定起始點,終點,時間,動畫會沿著你這設定點進行移動。可以看做特殊的cakeyframeanimation

(2) cakeyframeanimation

關鍵幀動畫

(3) caanimationgroup

組動畫,支援多個cabasicanimation或者cakeyframeanimation動畫同時執行

基本動畫**:

/*

*建立calayer

*/calayer * testlayer =[[calayer alloc] init];

testlayer.backgroundcolor =[uicolor greencolor].cgcolor;

testlayer.frame = cgrectmake(100, 100, 100, 100

); testlayer.cornerradius = 10

; [self.view.layer addsublayer:testlayer];

/**建立動畫

*/cabasicanimation * testanimiation = [cabasicanimation animationwithkeypath:@"

transform.scale"];

testanimiation.fromvalue = [nsnumber numberwithfloat:1.0

]; testanimiation.tovalue = [nsnumber numberwithfloat:1.5

]; testanimiation.autoreverses =yes;

testanimiation.fillmode =kcafillmodebackwards;

testanimiation.removedoncompletion =no;

testanimiation.repeatcount =maxfloat;

testanimiation.duration = 1

; [testlayer addanimation:testanimiation forkey:

@"testanimiation

"];

引數解釋:

1.animationwithkeypath的值如下:  

properties.   

翻轉包含scale rotation 

大部分我們常用的是:

transform.scale = 比例縮放動畫 

transform.scale.x = 寬的比例動畫 

transform.scale.y = 高的比例動畫 

transform.rotation.z = 平面的旋轉 

opacity = 透明度

2.fromvalue: 動畫的起始狀態值,雖然ios文件給出的型別是id,不過這裡應該傳nsvalue物件,比如nsnumber(nsnubmer繼承自nsvalue)。其具體含義

3.autoreverse: 當動畫執行到tovalue指定的狀態時是從tovalue的狀態逆回去,還是直接跳到fromvalue的狀態再執行一遍

4.filemode: fillmode的作用就是決定當前物件過了非active時間段的行為. 非active時間段是指動畫開始之前以及動畫結束之後。如果是乙個動畫caanimation,則需要將其removedoncompletion設定為no,要不然fillmode不起作用. 下面來講各個fillmode的意義:

kcafillmoderemoved 這個是預設值,也就是說當動畫開始前和動畫結束後,動畫對layer都沒有影響,動畫結束後,layer會恢復到之前的狀態

kcafillmodeforwards 當動畫結束後,layer會一直保持著動畫最後的狀態

kcafillmodebackwards 這個和kcafillmodeforwards是相對的,就是在動畫開始前,你只要將動畫加入了乙個layer,layer便立即進入動畫的初始狀態。因為有可能出現fromvalue不是目前layer的初始狀態的情況,如果fromvalue就是layer當前的狀態,則這個引數就沒太大意義。

kcafillmodeboth 理解了上面兩個,這個就很好理解了,這個其實就是上面兩個的合成.動畫加入後開始之前,layer便處於動畫初始狀態,動畫結束後layer保持動畫最後的狀態.

caanimationgroup實現動畫

/*

*建立calayer

*///

calayer * testlayer = [[calayer alloc] init];

= [uicolor greencolor].cgcolor;

= cgrectmake(100, 100, 100, 100);

= 10;

//[self.view.layer addsublayer:testlayer];

uilabel * label = [[uilabel alloc] initwithframe:cgrectmake(100, 100, 100, 100

)]; label.backgroundcolor =[uicolor greencolor];

[self.view addsubview:label];

/**建立動畫

*//*

*縮放*/

cabasicanimation * testanimiation = [cabasicanimation animationwithkeypath:@"

transform.scale"];

testanimiation.fromvalue = [nsnumber numberwithfloat:1.0

]; testanimiation.tovalue = [nsnumber numberwithfloat:1.5

]; testanimiation.autoreverses =yes;

testanimiation.fillmode =kcafillmodebackwards;

testanimiation.removedoncompletion =no;

testanimiation.repeatcount =maxfloat;

testanimiation.duration = 1;//

[label.layer addanimation:testanimiation forkey:@"testanimiation"];

/**移動

*/cabasicanimation *moveanimation = [cabasicanimation animationwithkeypath:@"

position"];

moveanimation.fromvalue =[nsvalue valuewithcgpoint:label.layer.position];

moveanimation.tovalue = [nsvalue valuewithcgpoint:cgpointmake(320 - 80

, label.layer.position.y)];

moveanimation.autoreverses =yes;

moveanimation.repeatcount =maxfloat;

moveanimation.duration = 2

;

/**旋轉

*/cabasicanimation *rotateanimation = [cabasicanimation animationwithkeypath:@"

transform.rotation.x"];

rotateanimation.fromvalue = [nsnumber numberwithfloat:0.0

]; rotateanimation.tovalue = [nsnumber numberwithfloat:6.0 *m_pi];

rotateanimation.autoreverses =yes;

rotateanimation.repeatcount =maxfloat;

rotateanimation.duration = 2

;

/**動畫組合

*/caanimationgroup *groupannimation =[caanimationgroup animation];

groupannimation.duration = 2

; groupannimation.autoreverses =yes;

groupannimation.animations =@[moveanimation, testanimiation, rotateanimation];

groupannimation.repeatcount =maxfloat;

[label.layer addanimation:groupannimation forkey:

@"groupannimation

"];

mac開發 ios開發

但是,任何乙個作業系統上,只使用開發語言就去開發程式是不行的。還需要有介面庫。尤其是支援object c的介面庫。mac上使用oc開發應用程式,都會使用xcode這個ide,整合開發工具,xcode中整合了gui介面庫。可以直接拖動控制項到介面上。objective c是一門語言,而cocoa 是這...

IOS開發經驗

基本要點 首先,你的應用程式 不能導致手機故障 比如崩潰或螢幕問題 應用內的所有 資訊中不能用固定值代替可變變數 不要使用任何sdk裡面的私人api 不要使用任何sdk文件裡面沒有列出的功能 不要提及使用者裝置上不存在的硬體功能 如果需要網路連線,在沒有網路的情況下要告知使用者 不要 過度 模仿任何...

ios開發記事

1.loadview 和 viewdidload 區別 就是當view的nib檔案為nil時,手工建立檢視介面時呼叫loadview 當view的nib檔案存在的時候,初始化工作在viewdidload中實現 但是如果你的程式執行期間記憶體不足,檢視控制器接到didreceivememorywarn...