- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法淺析


轉載自:http://blog.csdn.net/ronaldo_carry/article/details/49070119

 

將viewdidload里面的代碼全部注釋掉

 

- (void)viewDidLoad {

    [superviewDidLoad];

}

 

 

重寫點擊交換的事件方法

 

//交換視圖

- (IBAction)changeBtn {

    //通過類方法來創建轉場動畫

    CATransition *transition = [CATransitionanimation];

    transition.duration = 1.0f;//動畫的間隔為1S

    //設置動畫的變化方法

    transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    //下面是交換視圖特有的屬性

    //私有API 使用私有API的時候要慎重(這個是蘋果官網承認的)

    transition.type = @"pageCurl";

//    transition.type =@"fade";fade漸隱

    //type的子類型 轉換的方向

    transition.subtype =  @"fromRight";

//    //這里也可以寫成這樣 效果是一樣的

//    transition.subtype = kCATransitionFromRight;

    //設置具體的動畫 交換兩個視圖的位置

    [_aninmationViewexchangeSubviewAtIndex:0withSubviewAtIndex:1];

    //給圖層添加動畫

    [_aninmationView.layeraddAnimation:transitionforKey:@"myAnimation"];

}

 

這樣運行出來的效果是  點擊了交換按鈕  出來動畫翻頁效果  但是始終展現的是最上面的view 並不會實現預期的交換兩個view的效果

查看了一下這個函數的官方文檔

- (void)addAnimation:(CAAnimation *)anim forKey:(nullableNSString *)key;

我們可以看到文檔中是這么解釋的:

 

/** Animation methods. **/

 

/* Attach an animation object to the layer. Typically this is implicitly

 * invoked through an action that is an CAAnimation object.

 *

 * 'key' may be any string such that only one animation per unique key

 * is added per layer. The special key 'transition' is automatically

 * used for transition animations. The nil pointer is also a valid key.

 *

 * If the `duration' property of the animation is zero or negative it

 * is given the default duration, either the value of the

 * `animationDuration' transaction property or .25 seconds otherwise.

 *

 * The animation is copied before being added to the layer, so any

 * subsequent modifications to `anim' will have no affect unless it is

 * added to another layer. */

 

翻譯過來就是:

/** Animation methods. **/  動畫方法

 

/* 給圖層(layer)附加上一個動畫對象  通常這是隱式的通過一個動作,這個動作是一個CAAnimation對象來調用.

 * 'key'鍵可能是任意的string,這樣每個唯一的鍵只有一個動畫(animation)被添加到圖層(layer)中.特殊的鍵'transition'會被自動用於轉場動畫中,空指針同樣也是一個空的鍵值.

 *如果動畫的持續時間(duration)屬性是0或者負,則給定默認時間,或者是轉換屬性`animationDuration'的值,否則的話0.25S.

 * 動畫(animation)被添加到圖層(layer)之前已經被復制(copied),所以任何后續對'anim'動畫修改都不會有影響,除非它被添加到另一個圖層(layer)中   /*這句話有點郁悶 沒怎么理解他說的意思*/

 **/

 

 

組動畫:

 

-(void)animationGroup{

CAAnimationGroup *animG = [CAAnimationGroup animation];

//2.添加動畫到動畫數組animation groups中

    animG.animations  = @[anim1,anim2];

 //4.添加動畫到圖層上

    [self.navigationController.view.layer addAnimation:animG forKey:@"animG"];

}

然后就會依次執行組動畫里的動畫了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM