Explanation of the animateKeyframesWithDuration method of UIView in iOS7

In iOS7, a method was added to UIView to use keyframe animation directly without using CoreAnimation, that is animateKeyframesWithDuration

The following is the source 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 {
    
    /**
     * when the relativeDuration animation starts
     * relativeStartTime the duration of the animation
     */
    
    [UIView animateKeyframesWithDuration:6.f
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionCalculationModeLinear
                              animations:^{
                                  [UIView addKeyframeWithRelativeStartTime:0.0 // Time relative to the start of 6 seconds (start animation at 0 seconds)
                                                          relativeDuration:1/3.0 // Relative to the duration of the 6 second animation (the animation lasts 2 seconds)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor redColor];
                                                                }];
                                  
                                  [UIView addKeyframeWithRelativeStartTime:1/3.0 // Time relative to the start of 6 seconds (the animation starts at the 2nd second)
                                                          relativeDuration:1/3.0 // Relative to the duration of the 6 second animation (the animation lasts 2 seconds)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor yellowColor];
                                                                }];
                                  [UIView addKeyframeWithRelativeStartTime:2/3.0 // Time relative to the start of 6 seconds (the animation starts at the 4th second)
                                                          relativeDuration:1/3.0 // Relative to the duration of the 6 second animation (the animation lasts 2 seconds)
                                                                animations:^{
                                                                    self.view.backgroundColor = [UIColor greenColor];                                                                }];
                                  
                              }
                              completion:^(BOOL finished) {
                                  [self runAnimateKeyframes];
                              }];
}

@end


Details:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326797582&siteId=291194637