LBTabBar.m
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// LBTabBar.m
// XianYu
//
// Created by li bo on 16/5/28.
// Copyright © 2016年 li bo. All rights reserved.
//
#import "LBTabBar.h"
#import <objc/runtime.h>
#import "UIImage+Image.h"
#import "UIView+LBExtension.h"
#define LBMagin 10
@interface LBTabBar ()
/** plus按钮 */
@property (nonatomic, weak) UIButton *plusBtn ;
@end
@implementation LBTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame]) {
// ----runtime - test----
// unsigned int count = 0;
// Ivar *ivarList = class_copyIvarList([UITabBar class], &count);
// for (int i =0; i<count; i++) {
// Ivar ivar = ivarList[i];
// LBLog(@"%s",ivar_getName(ivar));
// }
//[self setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]];
self.backgroundColor = [UIColor whiteColor];
[self setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
UIButton *plusBtn = [[UIButton alloc] init];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateHighlighted];
self.plusBtn = plusBtn;
[plusBtn addTarget:self action:@selector(plusBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:plusBtn];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
//系统自带的按钮类型是UITabBarButton,找出这些类型的按钮,然后重新排布位置,空出中间的位置
// Class class = NSClassFromString(@"UITabBarButton");
//
// self.plusBtn.centerX = self.centerX;
// //调整发布按钮的中线点Y值
// self.plusBtn.centerY = self.height * 0.5 - 2*LBMagin ;
//
// self.plusBtn.size = CGSizeMake(self.plusBtn.currentBackgroundImage.size.width, self.plusBtn.currentBackgroundImage.size.height);
//
//
// UILabel *label = [[UILabel alloc] init];
// label.text = @"发布";
// label.font = [UIFont systemFontOfSize:11];
// [label sizeToFit];
// label.textColor = [UIColor grayColor];
// [self addSubview:label];
// label.centerX = self.plusBtn.centerX;
// label.centerY = CGRectGetMaxY(self.plusBtn.frame) + LBMagin ;
//
//
//
// int btnIndex = 0;
// for (UIView *btn in self.subviews) {//遍历tabbar的子控件
// if ([btn isKindOfClass:class]) {//如果是系统的UITabBarButton,那么就调整子控件位置,空出中间位置
// //每一个按钮的宽度==tabbar的五分之一
// btn.width = self.width / 5;
//
// btn.x = btn.width * btnIndex;
//
// btnIndex++;
// //如果是索引是2(从0开始的),直接让索引++,目的就是让消息按钮的位置向右移动,空出来发布按钮的位置
// if (btnIndex == 2) {
// btnIndex++;
// }
//
// }
// }
//
// [self bringSubviewToFront:self.plusBtn];
}
//点击了发布按钮
- (void)plusBtnDidClick
{
//如果tabbar的代理实现了对应的代理方法,那么就调用代理的该方法
if ([self.delegate respondsToSelector:@selector(tabBarPlusBtnClick:)]) {
[self.myDelegate tabBarPlusBtnClick:self];
}
}
//重写hitTest方法,去监听发布按钮的点击,目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//这一个判断是关键,不判断的话push到其他页面,点击发布按钮的位置也是会有反应的,这样就不好了
//self.isHidden == NO 说明当前页面是有tabbar的,那么肯定是在导航控制器的根控制器页面
//在导航控制器根控制器页面,那么我们就需要判断手指点击的位置是否在发布按钮身上
//是的话让发布按钮自己处理点击事件,不是的话让系统去处理点击事件就可以了
if (self.isHidden == NO) {
//将当前tabbar的触摸点转换坐标系,转换到发布按钮的身上,生成一个新的点
CGPoint newP = [self convertPoint:point toView:self.plusBtn];
//判断如果这个新的点是在发布按钮身上,那么处理点击事件最合适的view就是发布按钮
if ( [self.plusBtn pointInside:newP withEvent:event]) {
return self.plusBtn;
}else{//如果点不在发布按钮身上,直接让系统处理就可以了
return [super hitTest:point withEvent:event];
}
}
else {//tabbar隐藏了,那么说明已经push到其他的页面了,这个时候还是让系统去判断最合适的view处理就好了
return [super hitTest:point withEvent:event];
}
}
@end