最近公司想做一個(gè)中間突出的按鈕素跺,做的過(guò)程中發(fā)現(xiàn)突出的地方總是點(diǎn)不中捧毛,所以記錄一下示罗。
代碼:在tabbar上面添加5個(gè)按鈕,中間的frame設(shè)置大一點(diǎn)妖啥,button添加點(diǎn)擊事件和平時(shí)相同燕雁。然后重寫(xiě)點(diǎn)擊函數(shù)在自己新建的tabbar中重寫(xiě)-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
诞丽,
代碼:
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView * view = nil;
if (self.isHidden == NO) {
for (UIView *subView in self.subviews) {
CGPoint testPoint = [subView convertPoint:point fromView:self];//轉(zhuǎn)換坐標(biāo)
//判斷是否view是否包含點(diǎn)擊事件
if (CGRectContainsPoint(subView.bounds, testPoint)) {
//self.centerButton是中間的按鈕,判斷點(diǎn)擊point是否在中間的按鈕范圍內(nèi)
if (CGRectContainsPoint(self.centerButton.frame,point)) {
return self.centerButton;
} else if ([subView isKindOfClass:[UIButton class]]) {
//判斷 其他的四個(gè)按鈕是否接受點(diǎn)擊事件
view = subView;
return view;
}else {
//除了按鈕之外,就讓父視圖去處理事件
UIView * superView=[super hitTest:point withEvent:event];
return superView;
}
}
}
//返回nil是不處理
return nil;
}else{
return nil;
}
}
添加按鈕到tabbar
- (void)setMMDItems
{
for (UIButton* button in _buttons) {
[button removeActionCompletionBlocksForControlEvents:UIControlEventTouchUpInside];
[button removeFromSuperview];
}
for (UILabel *label in _labels) {
[label removeFromSuperview];
}
_buttons = [NSMutableArray array];
_labels = [NSMutableArray array];
_badges = [NSMutableArray array];
int btnNum = 5;
for (int i = 0; i < btnNum; i++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* buttonImage = [self getImageWithTabIndex:i isPressed:NO];
UIImage* buttonPressedImage = [self getImageWithTabIndex:i isPressed:YES];
button.imageEdgeInsets = UIEdgeInsetsMake(-11, 0, 0, 0);
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:buttonImage forState:UIControlStateDisabled];
[button setImage:buttonPressedImage forState:UIControlStateHighlighted];
[button setImage:buttonPressedImage forState:UIControlStateSelected];
[self addSubview:button];
UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.contentMode = UIViewContentModeTop;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = COLORRGB(149, 149, 149);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:10];
titleLabel.text = [self getTabbarTitleWithTabIndex:i];
[self addSubview:titleLabel];
//CGFloat textOffset = 14;
int firstBtnWidth = kScreenWidth/btnNum;
UILabel *badge = [[UILabel alloc]init];
badge.layer.cornerRadius = 3.5;
badge.backgroundColor = [UIColor redColor];
badge.layer.masksToBounds = YES;
badge.hidden = YES;
[self addSubview:badge];
badge.frame = CGRectMake((i+1)*firstBtnWidth - 15 - 20, 7, 7, 7);
CGFloat height = 0.0f;//按鈕高度49
height = 49.0f;
if (i == 2) {
button.frame = CGRectMake(i*firstBtnWidth, -20, firstBtnWidth, height);
self.centerButton = button;
self.centerButton.acceptEventInterval = 1.5;//點(diǎn)擊頻率間隔1.5s
}else {
button.frame = CGRectMake(i*firstBtnWidth, 0, firstBtnWidth, height);
}
titleLabel.frame = CGRectMake(i*firstBtnWidth, 35, firstBtnWidth, 12);
[button addActionCompletionBlock:^(id sender) {
self.selectedIndex = i;
} forControlEvents:UIControlEventTouchUpInside];
if (i == self.selectedIndex) {
[button setImage:[button imageForState:UIControlStateSelected] forState:UIControlStateNormal];
titleLabel.textColor = COLORRGB(59, 171, 179);
}
[_buttons addObject:button];
[_labels addObject:titleLabel];
[_badges addObject:badge];
}
}