使用RDRDVTabBarController第三方方法,如何实现中间tab突出显示的效果

实现效果如下:

使用RDRDVTabBarController第三方方法,如何实现中间tab突出显示的效果_第1张图片


实现逻辑主要是在RDVTabBarController方法中添加一个UIButton,来遮住原来的TabItem

#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import 

@interface UIViewController (RDVTabBarControllerItemInternal)

- (void)rdv_setTabBarController:(RDVTabBarController *)tabBarController;

@end

@interface RDVTabBarController () {
    UIView *_contentView;
    UIButton *_centerBtn;
    CGRect _frameBtn;
}

@property (nonatomic, readwrite) RDVTabBar *tabBar;

@end

@implementation RDVTabBarController

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:[self contentView]];
    [self.view addSubview:[self tabBar]];
    
    NSUInteger index = self.viewControllers.count/2;
    RDVTabBarItem *item = self.tabBar.items[index];
    
    UIImage *image = item.finishedSelectedImage;
    CGSize imageSize = [image size];
    imageSize = CGSizeMake(imageSize.width/3*2, imageSize.height/3*2);
    
    CGRect frame = [UIScreen mainScreen].bounds;
    float width = frame.size.width/(self.viewControllers.count-1);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake((frame.size.width-width)/2, frame.size.height-width, width, width);
    btn.backgroundColor = KTabBarBackgroundColor;
    btn.layer.cornerRadius = width/2;
    btn.layer.borderWidth = 2;
    btn.layer.borderColor = HEX_COLOR(@"d0d0d8").CGColor;
    btn.adjustsImageWhenDisabled = NO;
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTintColor:KUnSelectedTitleAttributes];
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [btn setImageEdgeInsets:UIEdgeInsetsMake((width-imageSize.height)/2-10, (width-imageSize.width)/2, (width-imageSize.width)/2+10, (width-imageSize.width)/2)];
    if(WIDTH == 320){
        btn.titleLabel.font = [UIFont systemFontOfSize:11.0f];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(28, -30, 0, 5)];
    }else{
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(35, -33, 0, 5)];
    }
    btn.tag = index;
    [btn addTarget:self action:@selector(clickCenterBtn:) forControlEvents:UIControlEventTouchUpInside];
    _centerBtn = btn;
    [_centerBtn setAdjustsImageWhenHighlighted:NO];
    [self.view addSubview:_centerBtn];
    _frameBtn = _centerBtn.frame;
}

- (IBAction)clickCenterBtn:(UIButton*)sender{
    
    if ([[self delegate] respondsToSelector:@selector(tabBarController:shouldSelectViewController:)]) {
        if (![[self delegate] tabBarController:self shouldSelectViewController:[self viewControllers][sender.tag]]) {
            return;
        }
    }
    
    if ([self selectedViewController] == [self viewControllers][sender.tag]) {
        if ([[self selectedViewController] isKindOfClass:[UINavigationController class]]) {
            UINavigationController *selectedController = (UINavigationController *)[self selectedViewController];
            
            if ([selectedController topViewController] != [selectedController viewControllers][0]) {
                [selectedController popToRootViewControllerAnimated:YES];
            }
        }
        
        return;
    }
    
    
    [self setSelectedIndex:sender.tag];
    if ([[self delegate] respondsToSelector:@selector(tabBarController:didSelectViewController:)]) {
        [[self delegate] tabBarController:self didSelectViewController:[self viewControllers][sender.tag]];
    }
    [sender setTintColor:KSelectedTitleAttributes];
}

隐藏显示tabbar

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
    _tabBarHidden = hidden;
    
    __weak RDVTabBarController *weakSelf = self;
    
#ifdef HMC
    //上下出现
    void (^block)() = ^{
        CGSize viewSize = weakSelf.view.bounds.size;
        CGFloat tabBarStartingY = viewSize.height;
        CGFloat contentViewHeight = viewSize.height;
        CGFloat tabBarHeight = CGRectGetHeight([[weakSelf tabBar] frame]);
        
        if (!tabBarHeight) {
            tabBarHeight = 49;
        }
        
        if (!hidden) {
            tabBarStartingY = viewSize.height - tabBarHeight;
            if (![[weakSelf tabBar] isTranslucent]) {
                contentViewHeight -= ([[weakSelf tabBar] minimumContentHeight] ?: tabBarHeight);
            }
            [[weakSelf tabBar] setHidden:NO];
        }
        
        [[weakSelf tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
        [[weakSelf contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];
    };
#else
    //水平出现
    void (^block)() = ^{
        CGRect frame = [[weakSelf tabBar] frame];
        frame.origin.y = HEIGHT - 49;
        frame.size = CGSizeMake(WIDTH, 49);
        if(!hidden){
            _centerBtn.frame = _frameBtn;
            frame.origin.x = 0;
            [[weakSelf tabBar] setHidden:NO];
        }else{
            CGRect frameBtn = _frameBtn;
            frameBtn.origin.x -= WIDTH;
            _centerBtn.frame = frameBtn;
            frame.origin.x = -WIDTH;
        }
        [[weakSelf tabBar] setFrame:frame];
        [[weakSelf contentView] setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    };
#endif
    void (^completion)(BOOL) = ^(BOOL finished){
        if (hidden) {
            [[weakSelf tabBar] setHidden:YES];
        }
    };
    
    if (animated) {
        [UIView animateWithDuration:0.24 animations:block completion:completion];
    } else {
        block();
        completion(YES);
    }
}



你可能感兴趣的:(UI基础)