//
// ViewController.m
// TransitionFromView
//
// Created by YouXianMing on 16/5/30.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "ViewController.h"
typedef enum : NSUInteger {
kBottomView = 1000,
kTopView,
} EViewControllerTag;
@interface ViewController () {
UIView *redView;
UIView *yellowView;
UIView *containerView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// ContainerView
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 350)];
containerView.center = self.view.center;
[self.view addSubview:containerView];
// BottomView
redView = [[UIView alloc] initWithFrame:containerView.bounds];
redView.tag = kBottomView;
redView.backgroundColor = [UIColor redColor];
[containerView addSubview:redView];
// TopView
yellowView = [[UIView alloc] initWithFrame:containerView.bounds];
yellowView.tag = kTopView;
yellowView.backgroundColor = [UIColor yellowColor];
[containerView addSubview:yellowView];
// Button
UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];
[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonEvent:(UIButton *)button {
button.enabled = NO;
[UIView transitionFromView:[containerView viewWithTag:kTopView]
toView:[containerView viewWithTag:kBottomView]
duration:0.5f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
button.enabled = YES;
if ([[containerView viewWithTag:kBottomView] isEqual:redView]) {
[containerView insertSubview:yellowView belowSubview:redView];
redView.tag = kTopView;
yellowView.tag = kBottomView;
} else {
[containerView insertSubview:redView belowSubview:yellowView];
redView.tag = kBottomView;
yellowView.tag = kTopView;
}
}];
}
@end