您的位置:首页 > 移动开发 > IOS开发

IOS学习之——导航2 模态的原理和实现

2015-01-29 11:44 323 查看

什么是模态?


这是个问题,什么是模态呢?简单的说,就是你在旅游,突然老板来电话了找你解决技术问题,很急。所以你必须先搞定这个电话,然后才能接着旅游。

又或者你要看视频,网站弹出如下窗口,你必须安装软件才能接着看……



所以模态,是一种状态,你必须先解决掉这个状态,才能继续下去。

实现模态

在IOS中,模态的应用可以是点击注册页,然后注册完成回到原来页面



通过storyboard获取对象

storyboardID的定义如下:

//    An identifier string that uniquely identifies the view controller in the storyboard file. You set the identifier for a given view controller in Interface Builder when configuring the storyboard file. This identifier is not a property of the view controller object itself and is used only by the storyboard file to locate the view controller.


翻译:这个标志字符串,是故事板中试图控制器的独特的标识符。
你可以在故事板中设置这个标识符,这个标识符并不是视图控制器的属性,仅仅使用在故事版中读取视图控制器的时候。
也就是说:storyboardID用于获取和构建storyboard中的viewController对象

获取storyboard对象

// 获取storyboard 对象
    UIStoryboard *mainStoryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    // 利用storyBoard ID 创建uiviewcontroller 对象
    UIViewController* model=[mainStoryBoard instantiateViewControllerWithIdentifier:@"ModelViewController"];


第一个函数:storyboardWithName: bundle:
试用特定的字符串,创建并且返回storyboard对象:返回的对象是下图main.storyboard



第二个函数:使用指定的标识符创建ViewController的实例,标识为:storyboardID,在属性页面设置



转换动画类型



接下来设置转换动画类型: (transition:过度)

// 过度动画
    model.modalTransitionStyle=UIModalTransitionStylePartialCurl;


有四种可选的类型:

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl NS_ENUM_***AILABLE_IOS(3_2),
};
我们来逐条解释一下:

1 cover: 包括,覆盖 Vertical:垂直的 翻译过来就是——垂直覆盖

2 flip:掷,弹出 Horizontal:水平的 翻译过来是——水平扔出,(这个好豪放……)

3 cross:交叉 dissolve:溶解 翻译过来是——交叉溶解(大约就是隐约透出来,逐渐明显)

4 partial:局部的 Curl:卷曲 翻译中文——掀起一角(自己脑补)

打开和关闭模态

1 打开模态 代码如下:
[self presentViewController:model animated:YES completion:^{
        NSLog(@"打开模态,就这么简单");
    }];


这个函数定义在UIViewController 类里面:意思是使用一个controller 作为自己的模态,后面的参数是一个block,里面的代码伴随着函数执行而执行,有兴趣同学可以自己搜索一下

2 关闭模态 代码如下
- (IBAction)closeModel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"模态结束");
    }];
}


这个函数也是定义在UIViewController类里面,意思是结束自己这个Controller的模态状态,返回原来Controller

效果图:





相关代码

https://git.oschina.net/zhengaoxing/navigation

欢迎转载

本文地址:http://blog.csdn.net/zhenggaoxing/article/details/43269109

前一篇:

IOS学习之——导航1 概述

IOS学习系列目录:

IOS学习系列目录——不定时更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: