柠盟科技实习第二个项目-Hi客

今天被老大告知又要新开一个项目,也就是说要同时写两个项目了。新开一篇文章,记录一下这个项目的开发过程。

1.超大TabBarButtonItem

Screen Shot 2018-03-19 at 20.43.07.png

根据用户的要求,tabBar中间的barButtonItem需要超出tabBar。在网上搜了些资料,很快就实现了。

思路大致如下:

  1. 实现一个UITabBar子类,在子类中,需要定义一个protocol,需要在tabBarController中实现该protocol,用于当点击自定义大button时,将selectedIndex设为中间index
    核心代码如下:
@protocol HKTabBarDelegate 

@required
- (void)cancelLastSelectedIndex;  // 将中间的buttonItem设为选中状态。
@end

@property (nonatomic, weak) id hkTabarDelegate;


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIButton *plusBtn = [[UIButton alloc] init];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"post_normal"]
                           forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"post_normal"]
                           forState:UIControlStateSelected];
        [plusBtn addTarget:self
                    action:@selector(plusButtonClicked:)
          forControlEvents:UIControlEventTouchUpInside];
        self.plusButton = plusBtn;
        
        [self addSubview:plusBtn];
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //系统自带的按钮类型是UITabBarButton
    Class class = NSClassFromString(@"UITabBarButton");
    
    // 设置plusButton的frame
    CGPoint center = self.center;
    center.x = self.center.x;
    center.y = self.frame.size.height * 0.5 - 2 * kCenterYOffset;
    self.plusButton.center = center;
    
    CGFloat buttonImageWidth = self.plusButton.currentBackgroundImage.size.width;
    CGFloat buttonImageHeight = self.plusButton.currentBackgroundImage.size.height;
    CGRect frame = self.plusButton.frame;
    frame.size = CGSizeMake(buttonImageWidth, buttonImageHeight);
    self.plusButton.frame = frame;
    
    int index = 0;
    for (UIView *button in self.subviews) {
        if ([button isKindOfClass:class]) {

            // 调整系统tabBarButton的位置
            CGFloat buttonXPoint = button.frame.size.width * index;
            CGFloat buttonYPoint = button.frame.origin.y;
            CGFloat buttonWidth = self.frame.size.width / 3;
            CGFloat buttonHeight = button.frame.size.height;
            button.frame = CGRectMake(
                                      buttonXPoint,
                                      buttonYPoint,
                                      buttonWidth,
                                      buttonHeight
                                      );
            
            index++;
        }
    }

    [self bringSubviewToFront:self.plusButton];
}



#pragma mark - ---------------------------------------Events, Callbacks---------------------------------------
#pragma mark 视图点击事件
//去监听发布按钮的点击,目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    //在导航控制器根控制器页面,那么我们就需要判断手指点击的位置是否在发布按钮身上
    //是的话让发布按钮自己处理点击事件,不是的话让系统去处理点击事件就可以了
    if (!self.isHidden) {
        CGPoint newPoint = [self convertPoint:point
                                       toView:self.plusButton];
        
        BOOL isInPlusButton = [self.plusButton pointInside:newPoint
                                                 withEvent:event];
        
        if (isInPlusButton) {
            return self.plusButton;
        } else {
            return [super hitTest:point withEvent:event];
        }
    } else {
        return [super hitTest:point withEvent:event];
    }
}

#pragma mark 响应button事件
- (void)plusButtonClicked:(id)sender {
    if ([self.hkTabarDelegate respondsToSelector:@selector(cancelLastSelectedIndex)]) {
        [self.hkTabarDelegate cancelLastSelectedIndex];
    }
}

    // 修改系统的tabBar
    HKTabBar *tabBar = [[HKTabBar alloc] init];
    [self setValue:tabBar forKeyPath:@"tabBar"];

你可能感兴趣的:(柠盟科技实习第二个项目-Hi客)