ios晉級之路-動畫CABaseAnimation

來源:互聯網
上載者:User

標籤:

     想在CALayer中實現動畫很容易,初學者可能會把思想局限於UIView層面上,其實不放用CALayer會比你想象的簡單且思路清晰,上篇隨筆中講到了CALayer的一些屬性,如果說你改變一些屬性比如bounds,position你會發現它是會內建隱式動畫的,而且效果不錯,不過在這裡你不能自訂動畫事件並且讓一組動畫有效執行。如果想實現上述的效果就需要CABaseAnimation

   //顯示動畫    CABasicAnimation * contentAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];    contentAnimation.fromValue = self.imageLayer.contents;    contentAnimation.toValue = (__bridge id)(image2.CGImage);    contentAnimation.duration = 1.0f;        //bounds動畫    CABasicAnimation * boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];    boundsAnimation.fromValue = [NSValue valueWithCGRect:self.imageLayer.bounds];    boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 1, 1)];    boundsAnimation.duration = 1.0f;        //組合動畫    CAAnimationGroup * grounp = [CAAnimationGroup animation];    grounp.animations = @[contentAnimation,boundsAnimation];    grounp.duration = 1.0f;        //設定layer動畫結束之後的值    self.imageLayer.bounds = CGRectMake(0, 0, 1, 1);    self.imageLayer.contents = (__bridge id)(image2.CGImage);    [self.imageLayer addAnimation:grounp forKey:nil];

如上代碼實現的是一個組合動畫,包括圖片的變換和Layer bounds的改變,在建立animation 的時候要寫入你要改變的屬性名稱,這個一定不要寫錯,fromValue需要寫入屬性的初始狀態,toValue需要寫入屬性變化後的值,輸入值都為對象,最後動畫是不會真的改變layer的屬性值的,如果不作處理動畫執行完後會變回初始狀態,想保留原來狀態需再最後賦值。

ios晉級之路-動畫CABaseAnimation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.