使用RNSwipeViewController类库进行视图切换

简介:

       如今很多应用已经不再局限于点击按钮触发事件来进行视图之间切换,为迎合给予用户更好体验,体现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方法中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- ( 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- ( 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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- ( 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;
     ;
                                                                                          
}


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

1
2
3
4
5
6
7
8
-( 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)

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

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

1
2
3
4
5
6
7
8
9
-( 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




     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1279125,如需转载请自行联系原作者




相关文章
|
1月前
|
JavaScript 前端开发 Linux
flutter开发-figma交互设计图可以转换为flutter源代码-如何将设计图转换为flutter源代码-优雅草央千澈
flutter开发-figma交互设计图可以转换为flutter源代码-如何将设计图转换为flutter源代码-优雅草央千澈
115 18
|
5月前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
80 3
|
5月前
|
前端开发 搜索推荐 Android开发
探索安卓开发中的自定义视图##
【9月更文挑战第6天】 在安卓应用开发的世界里,自定义视图如同绘画艺术中的色彩,它们为界面设计增添了无限可能。通过掌握自定义视图的绘制技巧,开发者能够创造出既符合品牌形象又提升用户体验的独特界面元素。本文将深入浅出地介绍如何从零开始构建一个自定义视图,包括基础框架搭建、关键绘图方法实现、事件处理机制以及性能优化策略。准备好让你的安卓应用与众不同了吗?让我们开始吧! ##
|
6月前
|
Android开发 UED 开发者
安卓开发中的自定义控件基础
【8月更文挑战第31天】在安卓应用开发过程中,自定义控件是提升用户界面和用户体验的重要手段。本文将通过一个简易的自定义按钮控件示例,介绍如何在安卓中创建和使用自定义控件,包括控件的绘制、事件处理以及与布局的集成。文章旨在帮助初学者理解自定义控件的基本概念,并能够动手实践,为进一步探索安卓UI开发打下坚实的基础。
|
文字识别 自然语言处理 程序员
一个支持WinForms换肤的开源组件
一个支持WinForms换肤的开源组件
75 0
|
前端开发 API Android开发
封装库/工具库中重要概念之字体图标
在前端开发中,使用图标是非常常见的需求。传统的图像格式(例如PNG和JPEG)虽然可以实现图标展示,但它们具有一些不足之处,如文件大小较大、缩放不清晰等等。字体图标则是一种新型的解决方案,它将图标以字符形式嵌入到Web页面中,并且具有矢量属性,因此可以无限缩放而不失真。在本文中,我们将探讨前端中一些常用的字体图标封装库和工具库。
112 0
|
前端开发 程序员 C#
Qt编写自定义控件70-扁平化flatui
一、前言 对于现在做前端开发人员来说,FlatUI肯定不陌生,最近几年扁平化的设计越来越流行,大概由于现在PC端和移动端的设备的分辨率越来越高,扁平化反而看起来更让人愉悦,而通过渐变色产生的质感色彩反而没有扁平化来得亲切。
1870 0
|
C# Android开发 iOS开发
C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码
原文:C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码 前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.
1314 0