iOS7中UIView的animateKeyframesWithDuration方法講解

iOS7中UIView的animateKeyframesWithDuration方法講解動畫

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

如下是使用源碼:code

//
//  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

細節之處:blog

相關文章
相關標籤/搜索