ios彩票之zidingyitabbar

 1 #import <UIKit/UIKit.h>
 2 
 3 @protocol NJTabBarDelegate <NSObject>
 4 /**
 5  *  @param from 从哪个视图(视图索引)
 6  *  @param to   到哪个视图(视图索引)
 7  */
 8 - (void)tabBarDidSelectBtnFrom:(NSInteger)from to:(NSInteger)to;
 9 
10 @end
11 
12 @interface NJTabBar : UIView
13 
14 @property (nonatomic, weak) id<NJTabBarDelegate> delegate;
15 
16 /**
17  *  提供给外界创建按钮
18  *
19  *  @param norName 默认状态的图片
20  *  @param disName 高亮状态的图片
21  */
22 - (void)addTabBarButtonWithNormalImageName:(NSString *)norName andDisableImageName:(NSString *)disName;
23 
24 @end

 

 1 #import "NJTabBar.h"
 2 #import "NJTabBarButton.h"
 3 
 4 @interface NJTabBar ()
 5 
 6 // 定义变量记录当前选中的按钮
 7 @property (nonatomic, weak) UIButton *selectBtn;
 8 
 9 @end
10 
11 @implementation NJTabBar
12 
13 
14 - (void)addTabBarButtonWithNormalImageName:(NSString *)norName andDisableImageName:(NSString *)disName
15 {
16     // 3.1创建按钮
17     NJTabBarButton *btn = [[NJTabBarButton alloc] init];
18     // 3.2设置按钮上显示的图片
19     // 3.2.1设置默认状态图片
20     [btn setBackgroundImage:[UIImage imageNamed:norName] forState:UIControlStateNormal];
21     
22     // 3.2.2设置不可用状态图片
23     [btn setBackgroundImage:[UIImage imageNamed:disName] forState:UIControlStateDisabled];
24     
25     // 3.4添加按钮到自定义TabBar
26     [self addSubview:btn];
27     
28     // 3.5监听按钮点击事件
29     [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
30     
31     // 3.6设置默认选中按钮
32     if (1 == self.subviews.count) {
33         [self btnOnClick:btn];
34     }
35     
36     // 3.7设置按钮高亮状态不调整图片
37     btn.adjustsImageWhenHighlighted = NO;
38 }
39 
40 - (void)layoutSubviews
41 {
42     [super layoutSubviews];
43     
44     for (int i = 0; i < self.subviews.count ; i++) {
45         
46         UIButton *btn = self.subviews[i];
47         
48         // 3.3设置frame
49         CGFloat btnY = 0;
50         CGFloat btnW = self.frame.size.width / self.subviews.count;
51         CGFloat btnH = self.frame.size.height;
52         CGFloat btnX = i * btnW;
53         btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
54         
55         // 3.8设置按钮的Tag作为将来切换子控制器的索引
56         btn.tag = i;
57     }
58 }
59 
60 - (void)btnOnClick:(UIButton *)btn
61 {
62 
63     // 3.切换子控制器
64     //    self.selectedIndex = btn.tag;
65     // 通知TabBarController切换控制器
66     if ([self.delegate respondsToSelector:@selector(tabBarDidSelectBtnFrom:to:)]) {
67         [self.delegate tabBarDidSelectBtnFrom:self.selectBtn.tag to:btn.tag];
68     }
69     
70 
71     
72     // 0.取消上一次选中的按钮
73     self.selectBtn.enabled = YES;
74     
75     // 1.设置当前被点击按钮为选中状态
76     btn.enabled = NO;
77     
78     // 2.记录当前选中的按钮
79     self.selectBtn = btn;
80     
81 }
82 
83 
84 @end

 

posted @ 2016-01-24 14:34  xiaocaoera  阅读(120)  评论(0编辑  收藏  举报