iOS7中UIView的animateKeyframesWithDuration方法講解


iOS7中UIView的animateKeyframesWithDuration方法講解

在iOS7中,給UIView添加了一個方法用來直接使用關鍵幀動畫而不用借助CoreAnimation來實現,那就是animateKeyframesWithDuration

以下是使用源碼:

//
//  ViewController.m
//
//  Created by YouXianMing on 14/11/26.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self runAnimateKeyframes];
}

- (void)runAnimateKeyframes {
    
    /**
     *  relativeDuration  動畫在什么時候開始
     *  relativeStartTime 動畫所持續的時間
     */
    
    [UIView animateKeyframesWithDuration:6.f
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionCalculationModeLinear
                              animations:^{
                                  [UIView addKeyframeWithRelativeStartTime:0.0   // 相對於6秒所開始的時間(第0秒開始動畫)
                                                          relativeDuration:1/3.0 // 相對於6秒動畫的持續時間(動畫持續2秒)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor redColor];
                                                                }];
                                  
                                  [UIView addKeyframeWithRelativeStartTime:1/3.0 // 相對於6秒所開始的時間(第2秒開始動畫)
                                                          relativeDuration:1/3.0 // 相對於6秒動畫的持續時間(動畫持續2秒)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor yellowColor];
                                                                }];
                                  [UIView addKeyframeWithRelativeStartTime:2/3.0 // 相對於6秒所開始的時間(第4秒開始動畫)
                                                          relativeDuration:1/3.0 // 相對於6秒動畫的持續時間(動畫持續2秒)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor greenColor];                                                                }];
                                  
                              }
                              completion:^(BOOL finished) {
                                  [self runAnimateKeyframes];
                              }];
}

@end

細節之處:

 


免責聲明!

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



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