iOS中UITabbarController左右滑动切换;UIViewController左右滑动;滑动切换;UITabbarController左右切换
转载请注明出处:http://blog.csdn.net/infant09/article/details/48773281
在经典的带有tabbar的app中,一般是引入一个UITabbarController,再添加多个UIViewController实现的。一般通过点击不同的UITabbarItem,实现不同的UIViewController切换。目前许多经典的app都是这样切换的,比如微信、微博以及iPhone自带的一些应用等等。
有时候也会有特殊的需求,比如要求左右滑动实现UIViewController的切换(例如android版的微信、QQ就可以这样,当然iOS版不这样,个人觉得可能和左右滑动会误操作有关?)。这时候如何实现呢?
思路1:在每个UIViewController中添加Pan手势,实现切换。
原理很简单,即为每个View添加Pan手势,能够左右滑动。当滑动过半的时候,tabbar切换页面。并且当每个ViewController DidAppear后,将左右两边的Controller渲染出来,添加到当前的View中(成为subView)。
下面给出一个例程。
0x00.新建一个project
选择新建一个“Tabbed Application”
0x01.补全storyboard和4个ViewController,并连接
0x02.对每个ViewController增加手势
做到左右滑动的关键点,即为每个ViewController的self.view增加一个Pan手势。以SecondViewController为例,在viewDidLoad中添加:
//添加左右滑动手势pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];
并添加handlePan:方法:
- (void) handlePan:(UIPanGestureR