iOS开发-iPad侧边栏Tab选项卡切换

十度 IOS 2015年12月01日 收藏

Android中习惯了叫侧边栏,iOS中如果不习惯侧边栏称呼的话可以叫dock,侧边栏的切换,类似于Android中的底部导航栏的切换,iPad尺寸大了一些,导航的栏目放在侧边会显示的更好耐看一些。选项卡是用按钮实现的,通过按钮的状态控制按钮的背景图片,最后通过按钮的Tag属性进行相对应的操作,iPad需要考虑一个横竖屏的问题,不过现在有些项目为了效果也好,为了开发效率也罢,可能只是选中了横屏效果。

基本布局

布局之前先来看一下最终需要实现的效果:

 

需要最四个图片进行相应的操作,通过图片控制最后的切换效果,黑色的属于侧边栏的区域,四个图片是按钮的背景图片,不过由于需要经常操作区域的宽度和按钮的宽度,需要预定义一下,新建一个Common.h文件,如果你不习惯,你也可以定义为Config.h,能看懂即可:

  1. //侧边栏条目的尺寸
  2. #define GPDockItemWidth 100
  3. #define GPDockItemHeight 80

在之前的xCode中是默认的有pch文件的,xCode6.1中没有,需要新建一个pch文件:

 

新建之后并不能保证你运行成功,还需要去编译中设置一下Prefix Header($(SRCROOT)/PrefixHeader.pch),清理下项目,导入Common.h文件即可成功;

Demo实战

①首先需要新建一个GPMainController控制器,控制页面页面逻辑:

 

 

  1. //
  2. // GPMainController.h
  3. // GrouponProject
  4. //http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/9.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import "GPDock.h"
  11.  
  12. @interface GPMainController : UIViewController <GPDockItemDelegate>
  13.  
  14.  
  15. @end

 

需要在ViewDidLoad加载侧边栏区域:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view.
  4. self.view.backgroundColor=[UIColor greenColor];
  5. //加入侧边栏Dock
  6. GPDock *dock=[[GPDock alloc]initWithFrame:CGRectMake(0, 0,GPDockItemWidth, self.view.frame.size.height)];
  7. dock.dockDelegate=self;
  8. [self.view addSubview:dock];
  9.  
  10. }

 响应侧边栏的点击事件,需要用到委托,如果委托不是很熟悉,可以参考本人之前的博客:

  1. -(void)switchMainByTabItem:(GPDock *)gpdock originalTab:(int)start destinationTab:(int)end{
  2. switch (end) {
  3. case 0:
  4. self.view.backgroundColor=[UIColor blackColor];
  5. break;
  6. case 1:
  7. self.view.backgroundColor=[UIColor blueColor];
  8. break;
  9. case 2:
  10. self.view.backgroundColor=[UIColor redColor];
  11. break;
  12. case 3:
  13. self.view.backgroundColor=[UIColor purpleColor];
  14. break;
  15. default:
  16. break;
  17. }
  18. }

GPMainContrller主要用于处理页面的逻辑,同时需要在AppDelegate中设置一下根控制器:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // Override point for customization after application launch.
  3. [UIView setAnimationDuration:2.0];
  4. self.window.rootViewController=[[GPMainController alloc]init];
  5. return YES;
  6. }

②设置侧边栏区域,继承自UIView:

  1. //
  2. // GPDock.h
  3. // GrouponProject
  4. //http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/10.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import "GPTabItem.h"
  11. @class GPDock;
  12. @protocol GPDockItemDelegate <NSObject>
  13.  
  14. -(void)switchMainByTabItem:(GPDock*)gpdock originalTab:(int)start destinationTab:(int)end;
  15.  
  16. @end
  17.  
  18. @interface GPDock : UIView
  19. {
  20. GPTabItem *selectedTabItem;
  21. }
  22. @property (nonatomic,weak) id<GPDockItemDelegate> dockDelegate;
  23.  
  24. @end

初始化侧边栏:

  1. -(instancetype)initWithFrame:(CGRect)frame{
  2. self=[super initWithFrame:frame];
  3. if (self) {
  4. //自动伸缩高度可伸缩,右边距可以伸缩
  5. self.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
  6. //设置背景图片
  7. self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Toolbar_bg_tabbar.png"]];
  8. [self addTabItems];
  9. }
  10. return self;
  11. }

 添加Tab选项卡:

  1. //添加Tab选项卡
  2. - (void)addTabItems
  3. {
  4. //首页
  5. [self addSingleTab:@"Toolbar_searchshop.png" selectedImage:@"Toolbar_searchshop_selected.png" weight:1];
  6. //团购
  7. [self addSingleTab:@"Toolbar_groupon.png" selectedImage:@"Toolbar_groupon_selected.png" weight:2];
  8.  
  9. //排行榜
  10. [self addSingleTab:@"Toolbar_ranklist.png" selectedImage:@"Toolbar_ranklist_selected.png" weight:3];
  11. // 个人中心
  12. [self addSingleTab:@"Toolbar_usercenter.png" selectedImage:@"Toolbar_usercenter_selected.png" weight:4];
  13. }

因为代码类似,所以封装到一个方法里面:

  1. - (void)addSingleTab:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage weight:(int)weight
  2. {
  3. GPTabItem *tabItem=[[GPTabItem alloc]init];
  4. [tabItem setBackgroundImage:backgroundImage];
  5. [tabItem setSelectedImage:selectedImage];
  6. //设置位置
  7. tabItem.frame = CGRectMake(0, GPDockItemHeight * (weight+1), 0, 0);
  8. //设置选中触摸选中事件
  9. [tabItem addTarget:self action:@selector(tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown];
  10. tabItem.tag = weight - 1;
  11. [self addSubview:tabItem];
  12. }

 设置触摸事件:

  1. //设置触摸事件
  2. - (void)tabItemTouchEvent:(GPTabItem *)tabItem
  3. {
  4.  
  5. if ([self.dockDelegate respondsToSelector:@selector(switchMainByTabItem:originalTab:destinationTab:)]) {
  6. [self.dockDelegate switchMainByTabItem:self originalTab:selectedTabItem.tag destinationTab:tabItem.tag];
  7. }
  8. selectedTabItem.enabled=YES;
  9. tabItem.enabled = NO;
  10. //将当前选中的赋值
  11. selectedTabItem =tabItem;
  12. }

③封装侧边栏的GPDockItem,然后选项卡上的可以继承:

  1. //
  2. // GPDockItem.h
  3. // GrouponProject
  4. //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/11.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface GPDockItem : UIButton
  12.  
  13. //背景图片
  14. @property (nonatomic,strong) NSString *backgroundImage;
  15. //选中图片
  16. @property (nonatomic,strong) NSString *selectedImage;
  17.  
  18. @end

 设置背景图片和选中图片:

  1. //
  2. // GPDockItem.m
  3. // GrouponProject
  4. //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/11.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "GPDockItem.h"
  10.  
  11. @implementation GPDockItem
  12.  
  13. /*
  14. // Only override drawRect: if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. - (void)drawRect:(CGRect)rect {
  17. // Drawing code
  18. }
  19. */
  20.  
  21. -(instancetype)initWithFrame:(CGRect)frame{
  22. self=[super initWithFrame:frame];
  23. if (self) {
  24. // Item分割线
  25. UIImageView *splitLine = [[UIImageView alloc] init];
  26. splitLine.frame = CGRectMake(0, 0, GPDockItemWidth, 2);
  27. splitLine.image = [UIImage imageNamed:@"separator_tabbar_item.png"];
  28. [self addSubview:splitLine];
  29. }
  30. return self;
  31.  
  32. }
  33. //设置背景图片
  34. -(void)setBackgroundImage:(NSString *)backgroundImage{
  35. _backgroundImage=backgroundImage;
  36. [self setImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];
  37. }
  38. //设置选中图片
  39. -(void)setSelectedImage:(NSString *)selectedImage{
  40. _selectedImage=selectedImage;
  41. [self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled];
  42. }
  43.  
  44. -(void)setFrame:(CGRect)frame{
  45. //固定Item宽高
  46. frame.size=CGSizeMake(GPDockItemWidth, GPDockItemHeight);
  47. [super setFrame:frame];
  48. }
  49.  
  50. @end

 GPTabItem代码:

  1. #import "GPDockItem.h"
  2.  
  3. @interface GPTabItem : GPDockItem
  4.  
  5. @end

设置选中时的背景图片:

  1. //
  2. // GPTabItem.m
  3. // GrouponProject
  4. //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/11.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "GPTabItem.h"
  10.  
  11. @implementation GPTabItem
  12.  
  13. /*
  14. // Only override drawRect: if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. - (void)drawRect:(CGRect)rect {
  17. // Drawing code
  18. }
  19. */
  20.  
  21. -(instancetype)initWithFrame:(CGRect)frame{
  22. self=[super initWithFrame:frame];
  23. if (self) {
  24. // 设置选中时背景图片
  25. [self setBackgroundImage:[UIImage imageNamed:@"bg_tabbar_item.png"] forState:UIControlStateDisabled];
  26. }
  27. return self;
  28. }
  29.  
  30. @end

 最终效果如下:

 代码相对以往较多,如有遗漏,请随时与我联系,如有好感,推荐或关注均可~