OC,oc語言

來源:互聯網
上載者:User

OC,oc語言
概述

  • 簡介

    • CAAnimationGroup又稱組動畫或動畫組
    • 將多個動畫放到動畫組中,並賦值給layer的animations屬性,動畫組中所有動畫就會並發執行
  • 注意事項

    • 動畫組中的動畫不會被壓縮,超齣動畫時間長度的部分將會被剪掉
    • 動畫組中的動畫的delegate與removedOnCompletion屬性將會被忽略
    • 由於忽略了removedOnCompletion屬性,動畫結束layer會恢複到動畫前的狀態
CAAnimationGroup唯一的屬性
  • animations(NSArray *)

    • 存放並發執行的所有動畫
    • 數組元素為CAAnimation的子類
樣本
  • 實現思路

    • 建立動畫組對象group
    • 建立多個CABasicAnimation對象,分別作用與不通過的屬性
    • 將多個基本動畫對象賦值給group的animations屬性
    • 將動畫組添加到要執行動畫的圖層上
    • 通過隱式動畫改變圖層的背景顏色
  • 實現步驟

    • 通過storyboard建立UIView控制項,並擁有它,修改它的class屬性為自訂的UIView的子類
    @property (weak, nonatomic) IBOutlet UIView *redView;
    • 建立動畫組
    //建立動畫組對象CAAnimationGroup *group = [CAAnimationGroup animation];//設定動畫組的執行時間group.duration = 1.0;//建立基本動畫對象,用來進行縮放CABasicAnimation *scale = [CABasicAnimation animation];scale.keyPath = @"transform.scale";scale.fromValue = @1.0;scale.toValue = @0.5;//建立基本動畫對象,用來進行旋轉CABasicAnimation *rotation = [CABasicAnimation animation];rotation.keyPath = @"transform.rotation";rotation.toValue = @(arc4random_uniform(M_PI * 2));//建立基本動畫對象,用來修改位置CABasicAnimation *position = [CABasicAnimation animation];position.keyPath = @"position";position.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200) + 20, arc4random_uniform(200) + 20)];//將基本動畫添加到動畫組中group.animations = @[scale, rotation, position];
    • 將動畫組添加到要執行動畫的圖層上
    [self.redView.layer addAnimation:group forKey:nil];
    • 通過隱式動畫改變layer的背景色
    self.redView.layer.backgroundColor = [self randomColor].CGColor;
    • 實現返回隨機顏色的方法
    //返回隨機顏色- (UIColor *)randomColor{return [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1.0];}

     

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.