VTMagic 的使用介绍
VTMagic
有很多开发者曾尝试模仿写出类似网易、腾讯等应用的菜单分页组件,但遍观其设计,大多都比较粗糙,不利于后续维护和扩展。琢磨良久,最终决定开源这个耗时近两年打磨而成的框架,以便大家可以快速实现类似需求,而不用把大量的精力浪费在重复造轮子的过程中,VTMagic目前在多个项目中稳定运行一年多。
特性概要
每个页面都是一个完整的控制器,友好支持个性化自定义;
页面切换时能准确触发相应的生命周期方法(viewWillAppear:等),便于管理各自页面的数据加载和其它逻辑处理;
导航栏支持多种布局样式,包括自适应文本宽度、自动平分、居中布局以及自定义宽度等;
可以在任意子控制器中,通过self.magicController获取最近的上层主控制器,方便跨层级处理逻辑;
支持内嵌webview,若滑动手势无法响应,可以通过handlePanGesture:解决;
支持页面重用和横竖屏切换;
更多特性请参见VTMagicView.h文件。
使用
VTMagic支持CocoaPods,只需在Podfile文件中添加如下代码即可:
pod "VTMagic"
集成
关于VTMagic的集成方法主要有以下两种:
1. 直接实例化VTMagicController对象,然后添加到当前控制器中。
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:self.magicController];
[self.view addSubview:_magicController.view];
[_magicController didMoveToParentViewController:self];
[_magicController.magicView reloadData];
}
- (VTMagicController *)magicController
{
if (!_magicController) {
_magicController = [[VTMagicController alloc] init];
_magicController.magicView.navigationColor = [UIColor whiteColor];
_magicController.magicView.sliderColor = [UIColor redColor];
_magicController.magicView.layoutStyle = VTLayoutStyleDivide;
_magicController.magicView.switchStyle = VTSwitchStyleDefault;
_magicController.magicView.navigationHeight = 40.f;
_magicController.magicView.dataSource = self;
_magicController.magicView.delegate = self;
}
return _magicController;
}
2. 继承VTMagicController,然后在viewDidLoad中完成相应配置。
#import "VTMagicController.h"
@interface ViewController : VTMagicController
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.magicView.navigationColor = [UIColor whiteColor];
self.magicView.sliderColor = [UIColor redColor];
self.magicView.layoutStyle = VTLayoutStyleDefault;
self.magicView.switchStyle = VTSwitchStyleDefault;
self.magicView.navigationHeight = 40.f;
self.magicView.dataSource = self;
self.magicView.delegate = self;
[self.magicView reloadData];
}
VTMagicViewDataSource协议
不管是通过以上哪种方法集成的,都需要实现数据源协议< VTMagicViewDataSource >,主要有以下三个方法:
- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView
{
return _menuList;
}
- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex
{
static NSString *itemIdentifier = @"itemIdentifier";
UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
if (!menuItem) {
menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
[menuItem setTitleColor:RGBCOLOR(50, 50, 50) forState:UIControlStateNormal];
[menuItem setTitleColor:RGBCOLOR(169, 37, 37) forState:UIControlStateSelected];
menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16.f];
}
return menuItem;
}
- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex
{
if (0 == pageIndex) {
static NSString *recomId = @"recom.identifier";
VTRecomViewController *recomViewController = [magicView dequeueReusablePageWithIdentifier:recomId];
if (!recomViewController) {
recomViewController = [[VTRecomViewController alloc] init];
}
return recomViewController;
}
static NSString *gridId = @"grid.identifier";
VTGridViewController *gridViewController = [magicView dequeueReusablePageWithIdentifier:gridId];
if (!gridViewController) {
gridViewController = [[VTGridViewController alloc] init];
}
return gridViewController;
}
集成效果
效果动态图太大此处无法展示,可点击原文进行查看
重要协议
除了数据源协议< VTMagicViewDataSource >外 ,VTMagic中的重要协议还有< VTMagicViewDelegate >和< VTMagicReuseProtocol >。前者用于在主控制器中处理页面切换事件;后者用于子控制器被重用时,清除旧数据等逻辑处理。
VTMagicViewDelegate协议
- (void)magicView:(VTMagicView *)magicView viewDidAppeare:(UIViewController *)viewController atPage:(NSUInteger)pageIndex
{
NSLog(@"pageIndex:%ld viewDidAppeare:%@",pageIndex, viewController.view);
}
- (void)magicView:(VTMagicView *)magicView viewDidDisappeare:(UIViewController *)viewController atPage:(NSUInteger)pageIndex
{
NSLog(@"pageIndex:%ld viewDidDisappeare:%@",pageIndex, viewController.view);
}
- (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex
{
NSLog(@"didSelectItemAtIndex:%ld", (long)itemIndex);
}
VTMagicReuseProtocol
- (void)vtm_prepareForReuse
{
NSLog(@"clear old data if needed:%@", self);
}
其它
你可以在任意子控制器中,通过self.magicController获取最近的上层主控制器,magicController遵循协议< VTMagicProtocol >,以便完成一些必要的跨层级的逻辑处理,前提是你需要import <VTMagic/VTMagic.h>文件。
NSInteger currentPage = self.magicController.currentPage;
UIViewController *viewController = self.magicController.currentViewController;
切换到指定页面,页面切换有两种方式:
[self.magicView switchToPage:3 animated:YES];
或者
[self.magicController switchToPage:3 animated:YES];
获取指定页面控制器,同样有两种方式:
UIViewController *viewController = [self.magicView viewControllerAtPage:3]
或者
UIViewController *viewController = [self.magicController viewControllerAtPage:3];
结束语
最后,按照惯例,如果你喜欢这个轮子,请留下一颗star,也欢迎大家扩散,这是对作者最大的鼓励和支持,拜谢!!!
GitHub地址:https://github.com/tianzhuo112/VTMagic
VTMagic 的使用介绍的更多相关文章
- 开源框架VTMagic的使用介绍
VTMagic 有很多开发者曾尝试模仿写出类似网易.腾讯等应用的菜单分页组件,但遍观其设计,大多都比较粗糙,不利于后续维护和扩展.琢磨良久,最终决定开源这个耗时近两年打磨而成的框架,以便大家可以快速实 ...
- CSS3 background-image背景图片相关介绍
这里将会介绍如何通过background-image设置背景图片,以及背景图片的平铺.拉伸.偏移.设置大小等操作. 1. 背景图片样式分类 CSS中设置元素背景图片及其背景图片样式的属性主要以下几个: ...
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- Linux下服务器端开发流程及相关工具介绍(C++)
去年刚毕业来公司后,做为新人,发现很多东西都没有文档,各种工具和地址都是口口相传的,而且很多时候都是不知道有哪些工具可以使用,所以当时就想把自己接触到的这些东西记录下来,为后来者提供参考,相当于一个路 ...
- JavaScript var关键字、变量的状态、异常处理、命名规范等介绍
本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...
- HTML DOM 介绍
本篇主要介绍DOM内容.DOM 节点.节点属性以及获取HTML元素的方法. 目录 1. 介绍 DOM:介绍DOM,以及对DOM分类和功能的说明. 2. DOM 节点:介绍DOM节点分类和节点层次. 3 ...
- HTML 事件(一) 事件的介绍
本篇主要介绍HTML中的事件知识:事件相关术语.DOM事件规范.事件对象. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三 ...
- HTML5 介绍
本篇主要介绍HTML5规范的内容和页面上的架构变动. 目录 1. HTML5介绍 1.1 介绍 1.2 内容 1.3 浏览器支持情况 2. 创建HTML5页面 2.1 <!DOCTYPE> ...
随机推荐
- UFLDL教程(四)之Softmax回归
关于Andrew Ng的machine learning课程中,有一章专门讲解逻辑回归(Logistic回归),具体课程笔记见另一篇文章. 下面,对Logistic回归做一个简单的小结: 给定一个待分 ...
- 【竞品分析】Android音乐播放器的竞品分析
迄今为止最长的一篇博客,各位看官笑纳~~ 本次分析基于Android平台,选取了几款我体验过的播放器进行比较分析.主要分为两类,一类是大而全的,功能全面,可满足用户管理歌曲.导入导出歌单等多方面需求, ...
- MATLAB中的结构数组
MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...
- E: Write error - write (28 No space left on device)
1:在终端中运行cd命令,提示: e: Write error - write (28 No space left on device) E: Cant mmap an empty file 2:使用 ...
- Delphi 版本号(D1到XE6),发现一个delphi.wikia.com网站
Borland Compiler Conditional Defines Edit Talk1 2,909PAGES ONTHIS WIKI Product Name Version Cond ...
- YII model模型和登陆详解
模型是 CModel 或其子类的实例.模型用于保持数据以及与其相关的业务逻辑. 模型是单独的数据对象.它可以是数据表中的一行,或者一个用户输入的表单. 数据对象的每个字段对应模型中的一个属性.每个属性 ...
- easy_install和pip区别
easy_insall的作用和perl中的cpan, ruby中的gem类似,都提供了在线一键安装模块的傻瓜方便方式,而pip是easy_install的改进版, 提供更好的提示信息,删除packag ...
- GPL,LGPL和BSD等协议注意事项
使用开源软件必须注意GPL,LGPL和BSD等协议 简而言之,GPL协议就是一个开放源代码协议,软件的初始开发者使用了GPL协议并公开软件的源程序后,后续使用该软件源程序开发软件者亦应当根据GPL协议 ...
- 如何解决缺少OCX问题,如何在win7 64位下注册OCX
最近原来的系统很慢,重装win7. 今天跑文章格式化编辑器,结果提示找不到Comctl32.ocx. 1. 上网搜索下载Comctl32.ocx,直接拷贝到c:\windows\system32不行, ...
- Eclipse添加快速查找Dao中方法所对应的Mybatis XML映射SQL的插件
Dao关联Mybatis快速查找的插件安装地址:http://dl.bintray.com/harawata/eclipse 安装步骤: ①Eclipse ==> Help ==> Ins ...