仿今日头条项目架构 (二)—— 项目主架构的搭建

版本记录

版本号 时间
V1.0 2017.11.11

前言

今日头条目前发展势头很猛,不仅有新闻资讯,还有视频,直播,微头条的模块,可以说,头条已经集成了新闻、社交、短视频和直播等多方面的技术和发展方向。接下来几篇我们就以头条为模板,对头条整体框架的搭建以及相关细节处理进行详细解析,希望大家能喜欢,有什么不对的地方希望大家能批评指正。感兴趣的可以看我写的上面几篇。详细代码已经长传至刀客传奇 - GitHub。
1. 仿今日头条项目架构 (一)—— ios11导航栏和tabBar层级

项目主架构

下面我们就看一下搭建的主架构,这里暂时包含自定义TabBar,以及四个主要的控制器。下面还是直接看代码。

1. JJTabBar.m
#import "JJTabBar.h"

@interface JJTabBar()

@property (nonatomic, strong) UIButton *composedButton;

@end

@implementation JJTabBar

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint(self.composedButton.frame, point)) {
        return self.composedButton;
    }
    else {
        return [super hitTest:point withEvent:event];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger tabBarButtonWidth = [UIScreen mainScreen].bounds.size.width * 0.2;
    NSInteger index = 0;
    
    for (UIView *value in self.subviews) {
        if ([value isKindOfClass:[NSClassFromString(@"UITabBarButton") class]]) {
            
            CGRect tabBarButtonFrame = value.frame;
            
            tabBarButtonFrame.origin.x = tabBarButtonWidth * index;
            tabBarButtonFrame.size.width = tabBarButtonWidth;
            value.frame = tabBarButtonFrame;
            index++;
            if (index == 2) {
                index++;
            }
        }
    }
    
    CGPoint composeButtonCenter = self.composedButton.center;
    composeButtonCenter.x = [UIScreen mainScreen].bounds.size.width * 0.5;
    composeButtonCenter.y = self.bounds.size.height * 0.3;
    
    self.composedButton.center = composeButtonCenter;
}

#pragma mark - Object Private Function

- (void)initUI
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"publish_tabbar"] forState:UIControlStateNormal];
    [button addTarget:self action: @selector(composeButtonDidCompose) forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    self.composedButton = button;
    [self addSubview:self.composedButton];
}
                        
#pragma mark - Action && Notification
                        
- (void)composeButtonDidCompose
{
    NSLog(@"中间按钮被点击了");
}

@end
2. JJTabBarVC.m
#import "JJTabBarVC.h"
#import "JJTabBar.h"
#import "JJNavigationVC.h"
#import "JJMineVC.h"
#import "JJMicroTouTiaoVC.h"
#import "JJVideoVC.h"
#import "JJHomeVC.h"

@interface JJTabBarVC ()

@property (nonatomic, strong) JJTabBar *customTabbar;

@end

@implementation JJTabBarVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    [self initUI];
}

#pragma mark - Object Private Function

- (void)initUI
{
    self.customTabbar = [[JJTabBar alloc] init];
    [self setValue:self.customTabbar forKey:@"tabBar"];
    
    [self addChildVCWithController:@"JJHomeVC" title:@"首页" imageName:@"home_tabbar_unselected" selectedImageName:@"home_tabbar_selected"];
    [self addChildVCWithController:@"JJVideoVC" title:@"西瓜视频" imageName:@"shortvideo_tabbar_unselected" selectedImageName:@"shortvideo_tabbar_selected"];
    [self addChildVCWithController:@"JJMicroTouTiaoVC" title:@"微头条" imageName:@"micro_tabbar_unselected" selectedImageName:@"micro_tabbar_selected"];
    [self addChildVCWithController:@"JJMineVC" title:@"我的" imageName:@"mine_tabbar_unselected" selectedImageName:@"mine_tabbar_selected"];
}

- (void)addChildVCWithController: (NSString *)vcName title: (NSString *)title imageName: (NSString *)imageName selectedImageName:(NSString *)selectImageName
{
    Class cls = NSClassFromString(vcName);
    UIViewController *vc = [[cls alloc] init];
    JJNavigationVC *nav = [[JJNavigationVC alloc] initWithRootViewController:vc];
    if ([vc isKindOfClass:[UIViewController class]]) {
        vc.title = title;
        vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [vc.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    }
    [self addChildViewController:nav];
}

@end

架构实现效果

下面我们就看一下实现的架构效果,慢慢我会完善。

仿今日头条项目架构 (二)—— 项目主架构的搭建_第1张图片
首页
仿今日头条项目架构 (二)—— 项目主架构的搭建_第2张图片
西瓜视频
仿今日头条项目架构 (二)—— 项目主架构的搭建_第3张图片
微头条
仿今日头条项目架构 (二)—— 项目主架构的搭建_第4张图片
我的

这里只是四个主控制器,下面我会继续完善,今日头条的完整架构。

后记

未完,待续~~~~

仿今日头条项目架构 (二)—— 项目主架构的搭建_第5张图片

你可能感兴趣的:(仿今日头条项目架构 (二)—— 项目主架构的搭建)