標籤:
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
細節之處:
iOS7中UIView的animateKeyframesWithDuration方法講解