GroupTabBar.m 2.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  GroupTabBar.m
//  垂直TabBar
//
//  Created by admin on 15/11/14.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "GroupTabBar.h"
#import "GroupItems.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height


#define kSelfWidth self.frame.size.width
#define kSelfHeight self.frame.size.height

18 19
#define kBtnTag 38341034

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
@interface GroupTabBar ()
@property (strong) NSMutableArray *buttons; // 存放所有button的数组
@end

@implementation GroupTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttons = [NSMutableArray array];
    }
    return self;
}

- (void)setItems:(NSArray *)items
{
    _items = items;
    CGFloat buttonHeight = kSelfHeight / _items.count;
    for (int i = 0; i < _items.count; i++) {
        GroupItems *item = _items[i];
        UIButton *button = [[UIButton alloc] init];
42
        button.tag = kBtnTag + i;
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
        button.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [button setTitleColor:kLightBlack forState:UIControlStateNormal];
        button.frame = CGRectMake(0, i * buttonHeight, kSelfWidth, buttonHeight);
        [button setTitle:item.title forState:UIControlStateNormal];
        [button addTarget:self action:@selector(touchDownForButton:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(touchUpForButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [self.buttons addObject:button];
    }
}


- (void)showIndex:(NSInteger) index
{
    [self touchDownForButton:[self.buttons objectAtIndex:index]];
    [self touchUpForButton:[self.buttons objectAtIndex:index]];
}

- (void)touchDownForButton:(UIButton *)button{
    button.selected = YES;
    NSInteger i = [self.buttons indexOfObject:button];
    
    UIView *view = [[self.items objectAtIndex:i] view];
    
    if (_delegate) {
        [_delegate switchView:view];
    }
    
    for (UIButton * b in self.buttons) {
        b.selected = NO;
    }
    
}
- (void)touchUpForButton:(UIButton *)button {
77
    self.selectNumber = (int)button.tag - kBtnTag;
78 79 80 81 82 83 84 85 86 87 88
    for (UIButton * b in self.buttons) {
        b.selected = NO;
        b.backgroundColor = kSectionBackGroundColor;
    }
    button.selected = YES;
    button.backgroundColor = [UIColor whiteColor];
}



@end