如今很多应用已经不再局限于点击按钮触发事件来进行视图之间切换,为迎合给予用户更好体验,体现iOS系统极佳用户体验,使用手势来进行各个视图之间切换,用户至于一个大拇指在屏幕中央就可浏览到很多信息;

关于 RNSwipeViewController: https://github.com/rnystrom/RNSwipeViewController

RNSwipeViewController是别人已经写好的一个ViewController容器,剩下我们要做的就是把自己的视图容器放到这个容器中进行管理。

首先学习 RNSwipeViewController里面的Demo

1.创建一个Single View Application工程,next,勾选 Use Storyboards,Use Automatic  Reference Counting

2.将RNSwipeViewController拖入新建到工程,添加QuartzCore.framework

3.新建四个类CenterViewController、LeftViewController、RightViewController、BottomViewController,继承UIViewController类

4.打开StoryBoard,从库里拖入四个ViewController视图控制器到StoryBoard里面,选中一个视图控制器设置类名和Storyboard ID,其他三个类似

5.在ViewController.h将加入#import "RNSwipeViewController.h"并将继承类改为RNSwipeViewController,在ViewDidLoad方法中

- (void)viewDidLoad
{
[super viewDidLoad];
CenterViewController *centerView = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:centerView];
centerView.title =@"Center";
self.centerViewController = centerNav;
self.leftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"]; self.rightViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"]; self.bottomViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"BottomViewController"]; }

如此我们就完成三个视图之间手势交互,首先出现的视图作为主视图,其他试图再是在它上面进行运动,手指向左滑右侧视图出现,向右滑动出现左视图,向上滑动出现底部视图出现

  

平常我们在构建一个带有XIB视图控制类的时候,初始化一般这样

CenterViewController *centerVC = [[CenterViewController alloc] initWithNibName:@"CenterViewController" bundle:nil];

但是在StoryBoard中,视图的Storyboard ID  成了这是视图的唯一标示,再给一个视图所属类时,设定好该视图的Storyboard ID,进行初始化时CenterViewController *centerView = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];

这个类库中也提供按钮点击进行视图交互方法,同时也设置视图显示宽度的属性,在类库实现的里面视图宽度有默认值

_leftVisibleWidth = 200.f;
_rightVisibleWidth = 200.f;
_bottomVisibleHeight = 300.0f;

再此我们可以在自己类里修改这个属性,根据自己需求,作图下设置

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; CenterViewController *centerView = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:centerView];
centerView.title =@"Center";
self.centerViewController = centerNav;
self.leftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"];
self.leftVisibleWidth = 100;
self.rightViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"];
self.rightVisibleWidth = self.view.frame.size.width;
self.bottomViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"BottomViewController"]; }

我们再给导航栏上添加两个按钮,在CenterViewController类中,包含#import "UIViewController+RNSwipeViewController.h"

- (void)viewDidLoad
{
[super viewDidLoad]; UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(0, 0, 44, 44);
[leftBtn setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(toggleLeft) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftBar
; UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(self.view.frame.size.width-44, 0,44 , 44);
[rightBtn setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(toggleRight) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = rightBar;
; }

接着连个按钮事件,为了显示明显我们可以设置一下三个视图背景颜色

-(void)toggleLeft
{
[self.swipeController showLeft];
} -(void)toggleRight
{
[self.swipeController showRight];
}

 

RNSwipeViewController有一个协议方法,可以监听当前视图显示百分比(0~100)

RNSwipeViewController have a protocol method, can monitor the current view shows percentage (0 ~ 100)

#import <UIKit/UIKit.h>
#import "RNRevealViewControllerProtocol.h"
@interface LeftViewController : UIViewController<RNRevealViewControllerProtocol> @end

协议方法,当左侧视图完全显示时弹出一个alertView

-(void)changedPercentReveal:(NSInteger)percent
{
if (percent == 100) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"这是一个测试" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show ]; }
}

源码下载地址:https://github.com/XFZLDXF/XFSwipeView.git

原创博客欢迎转载分享,请注明出处http://blog.csdn.net/duxinfeng2010

使用RNSwipeViewController类库进行视图切换的更多相关文章

  1. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  2. pushViewController addSubview presentModalViewController视图切换

    1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...

  3. IOS 视图切换动画

    我在网上找到的这个小方法,被我举一反三使用的屡试不爽.比如用在,当视图需要执行某一方法跳转到新的一个UIView上,从底层渐变浮到最上层.就是一个不错的视觉效果或者当需要类似keyboard的效果从底 ...

  4. UI3_视图切换

    // // ViewController.m // UI3_视图切换 // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 z ...

  5. UI2_视图切换ViewController

    // // SubViewController.h // UI2_视图切换 // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015 ...

  6. UI1_ViewController视图切换及Appdelegate

    // // ThirdViewController.h // UI1_ViewController视图切换及Appdelegate // // Created by zhangxueming on 1 ...

  7. UI2_视图切换

    // // ViewController.m // UI2_视图切换 // // Created by zhangxueming on 15/7/1. // Copyright (c) 2015年 z ...

  8. Tabbar视图切换,返回上一视图,添加item

    前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之 ...

  9. MFC视图切换大全总结

    单纯视图之间的切换 单文档多视图切换是我在学习MFC中遇到的一个老大难问题,在今天总算是一一破解了.我觉得视图切换分为三个等级,第一是在未切分窗格的情况下切换视图类:第二是在分割窗格的一个窗格内实行视 ...

随机推荐

  1. C#委托好处知多少

    1.性能 性能是泛型的一个主要优点. 直接上例子,通过实例可以让我们很好的理解这一点. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start() ...

  2. 利用MySQL 的GROUP_CONCAT函数实现聚合乘法

    MySQL 聚合函数里面提供了加,平均数.最小,最大等,可是没有提供乘法,我们这里来利用MYSQL现有的GROUP_CONCAT函数实现聚合乘法. 先创建一张演示样例表: CREATE TABLE ` ...

  3. 如何将UISearchBar上"Cancel"按钮改为”取消“?

    别说话,直接上代码 for (id obj in [searchBar subviews]) {        if ([obj isKindOfClass:[UIView class]]) {    ...

  4. SQL Server 基础 02 确保数据完整性

    本章总结目的:    为了巩固 约束.事务! 约 束 使用数据库约束就是保证数据库的完整性的方法,SQL Server 涉及的完整性有三个: 1.实体完整性 : (不能为空且重复,即唯一的,例如身份证 ...

  5. 彻底明白Java的IO系统

    java学习:彻底明白Java的IO系统 文章来源:互联网 一. Input和Output1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源.在Java的IO中,所有 ...

  6. GDSOI2015 task2 覆盖半径

    题目大意 一个\(n\times m\)的矩阵中有\(p\)个已经确定圆心的圆,并且每个格子有一定的分数,如果一个格子被任意一个或以上的圆覆盖,那么就可以得到这个格子的分数.现在求最小的半径,使得得分 ...

  7. CQOI2015 选数

    题目 从\([L, H]\)(\(H-L\leq 10^5\))选出\(n\)个整数,使得这些数的最大公约数为\(k\)的方案数. 算法 首先有一个很简单的转化,原问题可以简化为: 从\([\lcei ...

  8. Cocos2d-x CCTableView实现列表

    在ios程序设计中,会大量使用到tableview视图(UITableView),那么在cocos2d-x中,如果需要类似的列表,该如何实现呢?在引擎中参照ios中的UITableView实现了一个叫 ...

  9. 枚举+搜索 hdu-4431-Mahjong

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4431 题目大意: 给一副牌,求出所有能糊的牌. 解题思路: 枚举每一张牌,看能不能糊. 因为一共只有 ...

  10. Swift - 网络请求报App Transport Security has blocked a cleartext错

    使用Xcode7编写iOS9应用时,如果获取http://数据时会报如下错误: App Transport Security has blocked a cleartext HTTP (http:// ...