iOS 自定義tabbar+中間圓型大按鈕

最近公司想做一個中間突出的按鈕,做的過程中發(fā)現(xiàn)突出的地方總是點不中,所以記錄一下。
代碼:在tabbar上面添加5個按鈕,中間的frame設(shè)置大一點,button添加點擊事件和平時相同。然后重寫點擊函數(shù)在自己新建的tabbar中重寫-(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)換坐標
//判斷是否view是否包含點擊事件
            if (CGRectContainsPoint(subView.bounds, testPoint)) {
//self.centerButton是中間的按鈕,判斷點擊point是否在中間的按鈕范圍內(nèi)
                if (CGRectContainsPoint(self.centerButton.frame,point)) {
                    return self.centerButton;
                } else if ([subView isKindOfClass:[UIButton class]]) {
//判斷 其他的四個按鈕是否接受點擊事件
                    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;//點擊頻率間隔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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容