UIView key frame animation

UIView has animation features itself. Easy animation example is UIViewアニメーション(Japanese).
Basically, animation which is complicated should be implemented with Core Animation. But, we can implement easy animation by UIView features.
Above iOS7, UIView has key frame animation feature.
It makes codes of animation simpler.

Sample

This sample has 3 examples

  • UIView animation
  • Core Animation
  • UIView key frame animation

AnimationSampleViewController.h

@interface AnimationSampleViewController : UIViewController
@end

AnimationSampleViewController.m

@interface AnimationSampleViewController ()

@property (nonatomic) UIView *testView;
@property (nonatomic) UIButton *testButton;

@end

@implementation AnimationSampleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];    
    self.testButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    [self.testButton addTarget:self action:@selector(testAnim:) forControlEvents:UIControlEventTouchUpInside];
    [self.testButton setTitle:@"Start" forState:UIControlStateNormal];
    [self.view addSubview:self.testButton];
    
    self.testView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.testView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.testView];
}

#pragma mark - Event
-(void)testAnim:(id)sender {
    [self keyAnimTest];
}

-(void)easyAnimByUIView {
    // UIKit Animation
    
    [UIView animateWithDuration:1.0f animations:^{
     [self.testView setBackgroundColor:[UIColor greenColor]];
    }];
    
}

-(void)easyAnimByCoreAnim {
    // Core Animation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.duration = 1.0f;
    animation.toValue = (id)[UIColor greenColor].CGColor;
    [self.testView.layer addAnimation:animation forKey:@"backgroundColor"];
}


#pragma mark - Key Animation
-(void)keyAnimTest {
    // 2s Key Anim
    [UIView animateKeyframesWithDuration:2.0
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionCalculationModeLinear
                              animations:^{
                                  // Combination Animation
                                  // 0.0 - 2.0 * 0.5
                                  [UIView addKeyframeWithRelativeStartTime:0.0
                                                          relativeDuration:0.5   // relative
                                                                animations:^{
                                                                   [self.testView setBackgroundColor:[UIColor redColor]];
                                                                }];
                                  // 0.5 * 2 - 1.0 + 2 * 0.5
                                  [UIView addKeyframeWithRelativeStartTime:0.5  // Start
                                                          relativeDuration:0.5
                                                                animations:^{
                                                                    self.testView.frame = CGRectMake(100, 200, 100, 100);
                                                                }];
                                  
                                  
                              }
                              completion:^(BOOL finished) {
                                  NSLog(@"Animation was done");
                                  // Back
                                   self.testView.frame = CGRectMake(100, 100, 100, 100);
                                  [self.testView setBackgroundColor:[UIColor whiteColor]];
                              }];
}
@end

Result

This is animation. Easy testing is to use this ViewController as root viewcontroller

Ref

Apple Developer
[iOS 7] お手軽になったキーフレームアニメーション!