开源中国社区iOS客户端学习-(3)“技术问答”界面
发布日期:2022-02-01 13:46:26 浏览次数:28 分类:技术文章

本文共 6000 字,大约阅读时间需要 20 分钟。

在SideMenuViewController.m中可以找到(代码太长,需要拖动才能看到完整的)

case 0: {            SwipableViewController *newsSVC = [[SwipableViewController alloc] initWithTitle:@"技术问答"                                                                               andSubTitles:@[@"提问", @"分享", @"综合", @"职业", @"站务"]                                                                             andControllers:@[                                                                                              [[PostsViewController alloc] initWithPostsType:PostsTypeQA],                                                                                              [[PostsViewController alloc] initWithPostsType:PostsTypeShare],                                                                                              [[PostsViewController alloc] initWithPostsType:PostsTypeSynthesis],                                                                                              [[PostsViewController alloc] initWithPostsType:PostsTypeCaree],                                                                                              [[PostsViewController alloc] initWithPostsType:PostsTypeSiteManager]                                                                                              ]];            [self setContentViewController:newsSVC];            break;        }

其中case 0 对应的是“技术问答”界面,该界面可以分成两部分,如图

这里写图片描述
我们在工程文件中找到SwipableViewController,可以发现在它旁边还有两个类:TitleBarView(对应红色方框)和HorizonalViewController(对应蓝色方框)

TitleBarView

先看TitleBarView(我将说明部分放于代码的注释中,方便查看)

- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles{    self = [super initWithFrame:frame];    if (self) {        _currentIndex = 0;        _titleButtons = [NSMutableArray new];        CGFloat buttonWidth = frame.size.width / titles.count;//frame.size.width为ScrollView的宽        CGFloat buttonHeight = frame.size.height;        [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
//遍历,创建titleBar的button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor titleBarColor]; button.titleLabel.font = [UIFont systemFontOfSize:15]; [button setTitleColor:[UIColor colorWithHex:0x909090] forState:UIControlStateNormal]; [button setTitle:title forState:UIControlStateNormal]; button.frame = CGRectMake(buttonWidth * idx, 0, buttonWidth, buttonHeight); button.tag = idx; [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];//button添加点击事件 [_titleButtons addObject:button]; [self addSubview:button]; [self sendSubviewToBack:button]; }]; //点击放大 self.contentSize = CGSizeMake(frame.size.width, 25);//定义ScrollView内容视图的尺寸 self.showsHorizontalScrollIndicator = NO; //定义初始title的颜色和大小 UIButton *firstTitle = _titleButtons[0]; [firstTitle setTitleColor:[UIColor colorWithHex:0x009000] forState:UIControlStateNormal]; firstTitle.transform = CGAffineTransformMakeScale(1.15, 1.15); } return self;}

HorizonalViewController

再看一下HorizonalViewController,HorizonalViewController定义了滚动时的一些效果,具体的页面内容并不在这里定义

- (void)viewDidLoad{    [super viewDidLoad];    /***** 为解决iPhone 6 下的popviewcontroller后的问题而做的无奈之举,这样会引入新的问题,very ugly,亟待解决 *****/    self.tableView = [UITableView new];    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;    self.tableView.scrollsToTop = NO;    self.tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);//将view旋转角度pi/2    self.tableView.showsVerticalScrollIndicator = NO;//滚动条    self.tableView.pagingEnabled = YES;//确保页面一页页翻动    self.tableView.backgroundColor = [UIColor themeColor];    self.tableView.bounces = NO;    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHorizonalCellID];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dawnAndNightMode:) name:@"dawnAndNight" object:nil];}

我发现当注释掉

self.tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);//将view旋转角度pi/2

以及

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中的

cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);

显示内容的部分会被分成两截,不知道是不是代码中所说的

/***** 为解决iPhone 6 下的popviewcontroller后的问题而做的无奈之举,这样会引入新的问题,very ugly,亟待解决 *****/

SwipableViewController

主要看下面这个方法

- (instancetype)initWithTitle:(NSString *)title andSubTitles:(NSArray *)subTitles andControllers:(NSArray *)controllers underTabbar:(BOOL)underTabbar{    self = [super init];    if (self) {        self.edgesForExtendedLayout = UIRectEdgeNone;//UIRectEdgeAll全屏布局。UIRectEdgeNone不往四周扩展,按照iOS7之前的展示        //self.navigationController.navigationBar.translucent = NO;        //self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;        //设置title        if (title) {
self.title = title;} CGFloat titleBarHeight = 36; _titleBar = [[TitleBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, titleBarHeight) andTitles:subTitles]; _titleBar.backgroundColor = [UIColor clearColor]; [self.view addSubview:_titleBar]; //设置HorizonalTableView _viewPager = [[HorizonalTableViewController alloc] initWithViewControllers:controllers]; CGFloat height = self.view.bounds.size.height - titleBarHeight - 64 - (underTabbar ? 49 : 0); _viewPager.view.frame = CGRectMake(0, titleBarHeight, self.view.bounds.size.width, height); [self addChildViewController:self.viewPager]; [self.view addSubview:_viewPager.view]; __weak TitleBarView *weakTitleBar = _titleBar; __weak HorizonalTableViewController *weakViewPager = _viewPager; //滑动时,使titlebar和view一起动 _viewPager.changeIndex = ^(NSUInteger index) { weakTitleBar.currentIndex = index; ...... }; }

通过这三个类以及负责每隔页面具体内容的类就可以呈现“技术问答”了。

ps.个人水平有限,如果有误,望不吝赐教。

转载地址:https://blog.csdn.net/shidesheng5435/article/details/49469053 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:开源中国社区iOS客户端学习-(2)侧拉栏和设置界面
下一篇:开源中国社区iOS客户端学习-(4)下拉刷新

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月16日 02时06分02秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章