CAAnimation筆記

節(jié)選自:http://www.cnblogs.com/lee0oo0/p/4225730.html
設定動畫的屬性愉镰。以下是屬性及其對應的說明:

C221A1BE-B020-41BF-B75F-4324E1A0011E.png

animation.duration = 2.5; // 動畫持續(xù)時間  
animation.repeatCount = 1; // 不重復  
animation.beginTime = CACurrentMediaTime() + 2; // 2秒后執(zhí)行  
animation.autoreverses = YES; // 結束后執(zhí)行逆動畫  
// 動畫先加速后減速  
animation.timingFunction =  [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 

設定動畫開始和結束幀時的狀態(tài)肝集。設定的值會變?yōu)镵eyPath所指定的屬性的值囊嘉。
3F3702E4-5460-4680-B21F-FA95056C96DE.png
// 指定position屬性(移動)  
CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"position"];  
// 設定動畫起始幀和結束幀  
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始點  
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 終了點  
  • 縮放動畫(不停止)
    @property (weak, nonatomic) IBOutlet UIView *redView;
    @property (weak, nonatomic) IBOutlet UIImageView *imageV;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    // 創(chuàng)建動畫
    CABasicAnimation *anim = [CABasicAnimation animation];

          // 描述下修改哪個屬性產生動畫
          //    anim.keyPath = @"position";
          // 只能是layer屬性
          anim.keyPath = @"transform.scale";
          
          // 設置值
          //    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)];
          anim.toValue = @0.5;
          
          // 設置動畫執(zhí)行次數(shù)
          anim.repeatCount = MAXFLOAT;
          
          // 取消動畫反彈
          // 設置動畫完成的時候不要移除動畫
          anim.removedOnCompletion = NO;
          
          // 設置動畫執(zhí)行完成要保持最新的效果
          anim.fillMode = kCAFillModeForwards;
          
          [_imageV.layer addAnimation:anim forKey:nil];
      }
    

  • 手指畫一條線,讓view隨著這條線走
    @interface DrawView ()

      @property (nonatomic, strong) UIBezierPath *path;
      
      @end
      
      @implementation DrawView
      
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
          // touch
          UITouch *touch = [touches anyObject];
          
          // 獲取手指的觸摸點
          CGPoint curP = [touch locationInView:self];
          
          // 創(chuàng)建路徑
          UIBezierPath *path = [UIBezierPath bezierPath];
          _path = path;
          
          // 設置起點
          [path moveToPoint:curP];
          
      }
      
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
          // touch
          UITouch *touch = [touches anyObject];
          
          // 獲取手指的觸摸點
          CGPoint curP = [touch locationInView:self];
          
          [_path addLineToPoint:curP];
          
          [self setNeedsDisplay];
      }
      
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
          // 給imageView添加核心動畫
          // 添加核心動畫
          
          CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
          
          anim.keyPath = @"position";
          
          //    anim.values = @[@(angle2Radion(-10)),@(angle2Radion(10)),@(angle2Radion(-10))];
          
          anim.path = _path.CGPath;
          
          anim.duration = 1;
          
          anim.repeatCount = 1;
          //給這個view的圖層加一個動畫
          [[[self.subviews firstObject] layer] addAnimation:anim forKey:nil];
      }
      
      - (void)drawRect:(CGRect)rect
      {
          //根據(jù)_path畫一條線
          [_path stroke];
      }
      
      @end
    

  • 轉場動畫
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

      @end
      
      @implementation ViewController
      
      static int i = 1;
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
          
          // 轉場代碼
          if (i == 4) {
              i = 1;
          }
          // 加載圖片名稱
          NSString *imageN = [NSString stringWithFormat:@"%d",i++];
          _imageView.image = [UIImage imageNamed:imageN];
          
          // 轉場動畫
          CATransition *anim = [CATransition animation];
          anim.type = @"pageCurl";
          anim.duration = 0.5;
          
          [_imageView.layer addAnimation:anim forKey:nil];
      }
      
      @end
    

  • 動畫組
    @property (weak, nonatomic) IBOutlet UIView *redView;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    // 同時縮放樱拴,平移,旋轉
    CAAnimationGroup *group = [CAAnimationGroup animation];

      CABasicAnimation *scale = [CABasicAnimation animation];
      scale.keyPath = @"transform.scale";
      scale.toValue = @0.5;
    
      CABasicAnimation *rotation = [CABasicAnimation animation];
      rotation.keyPath = @"transform.rotation";
      rotation.toValue = @(arc4random_uniform(M_PI));
    
      CABasicAnimation *position = [CABasicAnimation animation];
      position.keyPath = @"position";
      position.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(200))];
    
      group.animations = @[scale,rotation,position];
    
      [_redView.layer addAnimation:group forKey:nil];
    }
    

  • 折疊(根據(jù)x旋轉)動畫
![DB8436BD-EE3B-42F6-AE6B-95091EF411C3.png](http://upload-images.jianshu.io/upload_images/1766523-d664e17fb6a46dfb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *topView;
    @property (weak, nonatomic) IBOutlet UIImageView *bottomView;
    // 添加手勢的View
    @property (weak, nonatomic) IBOutlet UIView *dragView;
    
    @property (nonatomic, weak) CAGradientLayer *gradientL;
    
    @end
    
    @implementation ViewController
    // 一張圖片必須要通過兩個控件展示,旋轉的時候,只旋轉上部分控件
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        // 通過設置contentsRect可以設置圖片顯示的尺寸整陌,取值0~1
        //topView和bottomView的frame一樣 通過設置鉚點anchorPoint來調整
        _topView.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
        _topView.layer.anchorPoint = CGPointMake(0.5, 1);
        
        _bottomView.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
        _bottomView.layer.anchorPoint = CGPointMake(0.5, 0);
        
        // 添加手勢
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        
        [_dragView addGestureRecognizer:pan];
        
        // 漸變圖層
        CAGradientLayer *gradientL = [CAGradientLayer layer];
        
        // 注意圖層需要設置尺寸
        gradientL.frame = _bottomView.bounds;
        
        gradientL.opacity = 0;
        gradientL.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
        _gradientL = gradientL;
        
        [_bottomView.layer addSublayer:gradientL];
    }
    
    // 拖動的時候旋轉上部分內容,200(垂直(Y)偏移量) M_PI(旋轉180度)
    - (void)pan:(UIPanGestureRecognizer *)pan
    {
        // 獲取偏移量
        CGPoint transP = [pan translationInView:_dragView];
        NSLog(@"%@", NSStringFromCGPoint(transP));
        // 旋轉角度,往下逆時針旋轉
        CGFloat angle = -transP.y / 200.0 * M_PI;
        
        CATransform3D transfrom = CATransform3DIdentity;
        
        
        // 增加旋轉的立體感瞎领,近大遠小,d:距離圖層的距離
        transfrom.m34 = -1 / 500.0;
        
        transfrom = CATransform3DRotate(transfrom, angle, 1, 0, 0);
        
        _topView.layer.transform = transfrom;
        
        // 設置陰影效果
        _gradientL.opacity = transP.y * 1 / 200.0;
        
        
        if (pan.state == UIGestureRecognizerStateEnded) { // 反彈
            /**
             * 彈簧效果的動畫
             * usingSpringWithDamping 參數(shù):  usingSpringWithDamping的范圍為0.0f到1.0f泌辫,數(shù)值越小「彈簧」的振動效果越明顯。
             * initialSpringVelocity 參數(shù): initialSpringVelocity則表示初始的速度九默,數(shù)值越大一開始移動越快震放。
             *  http://www.renfei.org/blog/ios-8-spring-animation.html
             */
            [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                _topView.layer.transform = CATransform3DIdentity;
                
            } completion:^(BOOL finished) {
                
            }];
        }
        
    }
    @end

  • 音量震動
E9E73E8C-2511-4656-94FC-9D9179D452B4.png
F8B483F8-D235-4CD6-A220-1BFBEF203356.png
   #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *lightView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // CAReplicatorLayer復制圖層,可以把圖層里面所有子層復制
        // 創(chuàng)建復制圖層
        CAReplicatorLayer *repL = [CAReplicatorLayer layer];
        repL.frame = _lightView.bounds;
        [_lightView.layer addSublayer:repL];
        
        //設置一個layer震動條
        CALayer *layer = [CALayer layer];
        layer.anchorPoint = CGPointMake(0.5, 1);
        layer.position = CGPointMake(15, _lightView.bounds.size.height);
        layer.bounds = CGRectMake(0, 0, 30, 150);
        layer.backgroundColor = [UIColor whiteColor].CGColor;
        [repL addSublayer:layer];
        
        //設置圖層的動畫
        CABasicAnimation *anim = [CABasicAnimation animation];
        anim.keyPath = @"transform.scale.y";
        anim.toValue = @0.1;
        anim.duration = 0.5;
        anim.repeatCount = MAXFLOAT;
        
        // 設置動畫反轉 動畫結束時是否執(zhí)行逆動畫
        anim.autoreverses = YES;
        [layer addAnimation:anim forKey:nil];
        
        // 復制層中子層總數(shù)
        // instanceCount:表示復制層里面有多少個子層荤西,包括原始層
        repL.instanceCount = 3;
        // 設置復制子層偏移量,不包括原始層,相對于原始層x偏移
        repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
        // 設置復制層動畫延遲時間
        repL.instanceDelay = 0.1;
        // 如果設置了原始層背景色伍俘,就不需要設置這個屬性
        repL.instanceColor = [UIColor redColor].CGColor;
        // 顏色值遞減
        repL.instanceRedOffset = -0.3;
    }
    
    @end

  • 菊花
45593372-4178-4EB6-925C-04765F9387AE.png
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *redView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        CAReplicatorLayer *repL = [CAReplicatorLayer layer];
        repL.frame = _redView.bounds;
        [_redView.layer addSublayer:repL];
        
        
        CALayer *layer = [CALayer layer];
        layer.transform = CATransform3DMakeScale(0, 0, 0);
        layer.position = CGPointMake(_redView.bounds.size.width / 2, 20);
        layer.bounds = CGRectMake(0, 0, 10, 10);
        layer.cornerRadius = 10.0;
        layer.masksToBounds = YES;
        layer.backgroundColor = [UIColor purpleColor].CGColor;
        [repL addSublayer:layer];
        
        // 設置縮放動畫
        CABasicAnimation *anim = [CABasicAnimation animation];
        anim.keyPath = @"transform.scale";
        anim.fromValue = @1;
        anim.toValue = @0;
        anim.repeatCount = MAXFLOAT;
        CGFloat duration = 1;
        anim.duration = duration;
        
        [layer addAnimation:anim forKey:nil];
        
        
        int count = 30;
        CGFloat angle = M_PI * 2 / count;
        // 設置子層總數(shù)
        repL.instanceCount = count;
        repL.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
        repL.instanceDelay = duration / count;
        
    }
    @end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末邪锌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子癌瘾,更是在濱河造成了極大的恐慌觅丰,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妨退,死亡現(xiàn)場離奇詭異妇萄,居然都是意外死亡蜕企,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門冠句,熙熙樓的掌柜王于貴愁眉苦臉地迎上來轻掩,“玉大人,你說我怎么就攤上這事懦底〈侥粒” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵聚唐,是天一觀的道長丐重。 經常有香客問我,道長杆查,這世上最難降的妖魔是什么扮惦? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮亲桦,結果婚禮上崖蜜,老公的妹妹穿的比我還像新娘。我一直安慰自己烙肺,他們只是感情好纳猪,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著桃笙,像睡著了一般氏堤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上搏明,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天鼠锈,我揣著相機與錄音,去河邊找鬼星著。 笑死购笆,一個胖子當著我的面吹牛,可吹牛的內容都是我干的虚循。 我是一名探鬼主播同欠,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼横缔!你這毒婦竟也來了铺遂?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤茎刚,失蹤者是張志新(化名)和其女友劉穎襟锐,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體膛锭,經...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡粮坞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年蚊荣,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片莫杈。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡互例,死狀恐怖,靈堂內的尸體忽然破棺而出姓迅,到底是詐尸還是另有隱情敲霍,我是刑警寧澤,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布丁存,位于F島的核電站肩杈,受9級特大地震影響,放射性物質發(fā)生泄漏解寝。R本人自食惡果不足惜扩然,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望聋伦。 院中可真熱鬧夫偶,春花似錦、人聲如沸觉增。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逾礁。三九已至说铃,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嘹履,已是汗流浹背腻扇。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留砾嫉,地道東北人幼苛。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像焕刮,于是被迫代替她去往敵國和親舶沿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果配并,實現(xiàn)這些動畫的過程并不復雜括荡,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,514評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果荐绝,實現(xiàn)這些動畫的過程并不復雜一汽,今天將帶大家一窺iOS動畫全貌避消。在這里你可以看...
    F麥子閱讀 5,116評論 5 13
  • 在iOS實際開發(fā)中常用的動畫無非是以下四種:UIView動畫低滩,核心動畫召夹,幀動畫,自定義轉場動畫恕沫。 1.UIView...
    請叫我周小帥閱讀 3,112評論 1 23
  • Core Animation Core Animation监憎,中文翻譯為核心動畫,它是一組非常強大的動畫處理API婶溯,...
    45b645c5912e閱讀 3,034評論 0 21
  • 一鲸阔、簡介 Core Animation,中文翻譯為核心動畫迄委,它是一組非常強大的動畫處理API褐筛,使用它能做出非常炫麗...
    莦婼姑娘閱讀 965評論 0 4