概述
视图控制器之间切换的时候默认是有动画的,如果我们想要定制一下,那么就用到导航栏控制动画了。
实例
演示各种视图控制器切换动画,由于种类较多,这里就 不截图了,可以一一尝试一下。
关于如何配置UIWindow,参看前面的文章。
实例代码
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain,nonatomic) UIWindow* window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "VCFirst.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
VCFirst* vcF = [VCFirst new];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vcF];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
@end
VCFirst.m
#import "VCFirst.h"
#import "VCSecond.h"
@interface VCFirst ()
@end
@implementation VCFirst
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setTitle:@"VCFirst"];
//添加一张图片
UIImage* image = [UIImage imageNamed:@"timg_1.jpg"];
UIImageView* iv = [[UIImageView alloc] initWithImage:image];
iv.frame = [UIScreen mainScreen].bounds;
[self.view addSubview:iv];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建层动画实例
CATransition* anim = [CATransition animation];
//动画时间
anim.duration = 1;
//动画类型:动画形式(缩放、位移、其他效果)
//如果不给导航控制器添加动画也是有默认动画的
//动画效果目前已经有比较多的了:rippleEffect\cube\moveIn\reveal\fade\pageCurl\pageUnCurl\suckEffect\rippleEffect\oglFlip
anim.type = @"reveal";
//动画子类型,例如动画方向
anim.subtype = kCATransitionFromRight;
//动画轨迹模式(线性、加速、减速等)
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//给导航栏设置动画
[self.navigationController.view.layer addAnimation:anim forKey:nil];
//点击跳转到视图控制器2
VCSecond* vcS = [VCSecond new];
[self.navigationController pushViewController:vcS animated:YES];
}
@end
VCSecond.m
#import "VCSecond.h"
@interface VCSecond ()
@end
@implementation VCSecond
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setTitle:@"VCSecond"];
//添加一张图片
UIImage* image = [UIImage imageNamed:@"timg_2.jpg"];
UIImageView* iv = [[UIImageView alloc] initWithImage:image];
iv.frame = [UIScreen mainScreen].bounds;
[self.view addSubview:iv];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//点击屏幕返回视图控制器1
[self.navigationController popViewControllerAnimated:YES];
}
@end