iOS入门-45导航栏控制动画

概述

视图控制器之间切换的时候默认是有动画的,如果我们想要定制一下,那么就用到导航栏控制动画了。

实例

演示各种视图控制器切换动画,由于种类较多,这里就 不截图了,可以一一尝试一下。
关于如何配置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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值