美文网首页程序员
让titleview与pageview分离 标题与分页控件分离

让titleview与pageview分离 标题与分页控件分离

作者: 莱姆船长 | 来源:发表于2018-05-03 15:14 被阅读0次

制作目的

  • 想要在特定的位置添加一个滚动标题栏

实现功能

  • 实现一个可以自由添加到某个 view 上的滚动标题栏,并与某个指定的 UICollectionView 进行联动

使用方式

#import "LGFTitles.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *pageView;
@property (weak, nonatomic) IBOutlet UIView *oneTitleSuperView;
@property (strong, nonatomic) LGFPageTitleView *oneTitleView;
@property (strong, nonatomic) NSMutableArray *oneTitles;
@end

@implementation ViewController

- (NSMutableArray *)oneTitles {
    if (!_oneTitles) {
        _oneTitles = [NSMutableArray arrayWithObjects:@"鹈鹕", @"鳄鱼", @"鲸鱼", @"一只大狮子", @"巨嘴鸟", @"麋鹿", @"绵羊", @"螃蟹", @"鸵鸟", @"大象", @"蛇", @"鱼", @"一只大公鸡", @"长颈鹿", @"猪", nil];
    }
    return _oneTitles;
}

// 固定宽度title, 下划线圆角添加, 右边title图片添加, 每个title添加不同图片效果展示, 少量title时title居中显示
- (LGFPageTitleView *)oneTitleView {
    if (!_oneTitleView) {
        LGFPageTitleStyle *style = [LGFPageTitleStyle na];
        // 固定宽度title 必要属性 (如果值为titleview宽度 / title数组count,titleview将取消滚动)
        style.title_fixed_width = self.view.width / 4;
        style.line_width_type = FixedWith;
        style.line_width = self.view.width;
        style.title_big_scale = 1.0;
        style.line_height = 5.0;
        style.line_bottom = 1.0;
        style.line_cornerRadius = style.line_height / 2;
        style.select_title_font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
        style.un_select_title_font = [UIFont fontWithName:@"Helvetica-Light" size:14];
        style.line_color = LGFRGB(155, 150, 255, 1.0);
        style.select_color = [UIColor greenColor];
        //-----如果添加不同图片使用这个属性---
        style.un_select_image_names = self.oneTitleUnSelectImages;
        style.select_image_names = self.oneTitleSelectImages;
        style.title_image_bundel = LGFBundle;
        //-----这些属性直接决定展示哪边图片----
        style.right_image_width = 20;
        //-----这些属性直接决定图片相对于title位置----
        style.right_image_spacing = 5.0;
        //----------------------------------
        //----少量title时title居中显示----
        style.is_title_center = YES;
        //--------------------------
        _oneTitleView = [[LGFPageTitleView na] initWithStyle:style super_vc:self super_view:self.oneTitleSuperView page_view:self.pageView];
    }
    return _oneTitleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加子控制器
    for (NSString *title in self.oneTitles) {
        ChildViewController *vc = [ChildViewController GETSBVC];
        vc.title = title;
        [self addChildViewController:vc];
        [self.childVCs addObject:vc];
    }
    // 刷新数据源
    [self.pageView reloadData];
    // 刷新title数组
    self.oneTitleView.style.titles = self.oneTitles;
    [self.oneTitleView reloadAllTitlesSelectIndex:10];
}

#pragma mark - Collection View 数据源 和 代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.childVCs.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    // cell请设置与 UICollectionView 同宽高
    return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"testcell" forIndexPath:indexPath];
    // 在cell上添加子控制器
    [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ChildViewController *vc = self.childVCs[indexPath.item];
    vc.view.frame = cell.bounds;
    [cell addSubview:vc.view];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    // 在此代理方法中作子控制器首次加载数据的操作 防止多余未出现的控制器加载数据而导致的卡顿 (只对将要出现的cell上的子控制器作加载数据操作,其余只加载控制器)
    ChildViewController *vc = self.childVCs[indexPath.item];
    if (vc.datas.count == 0) {// 如果该子控制器数据源数组为空那么执行请求数据
        LGFLog(@"正在刷新第%ld页", indexPath.row);
        [vc loadData];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // titleview与pageview分离关联关键方法,必须设置 (此方法作用:关联titleview和外部pageview, 使其可以联动)
    [self.oneTitleView autoScrollTitle];
}

@end

效果展示

LGFPageTitle

相关文章

  • 让titleview与pageview分离 标题与分页控件分离

    制作目的 想要在特定的位置添加一个滚动标题栏 实现功能 实现一个可以自由添加到某个 view 上的滚动标题栏,并与...

  • 064 Python语法之解耦(未完成)

    View与逻辑分离 逻辑与逻辑分离

  • 分离——情感分离与语言分离

    在关系当中,绝大多数都指向亲密,闺蜜、铁哥们,爱的像一个人一样……只有伟大的父母,在亲子关系中指向分离。 孩子有出...

  • 前端架构之路(1) - 前后端分离、web与static服务器分

    前后端分离、web与static服务器分离 1. 为什么需要 “前后端分离、web与static服务器分离” we...

  • 怎样理解内容与样式分离的原则

    怎样理解内容与样式分离的原则 在 Web 开发中,所谓内容与样式分离,就是让内容的归 HTML, 样式归 CSS,...

  • 南京行第六天

    今天是我们分离的日子,与人分离,与地点分离,却唯独没有与心分离。 早上我还在等待广播里传来熟悉的音乐,叫我起床时,...

  • 单纯性分离的落寞与无奈

    在几种分离的情形中,最无奈的莫过于单纯性分离吧。如今回想起自己与父母的分离应该就是这种分离,多了些无奈与落寞...

  • 分离

    天下似乎真的从来没有不散的宴席。每个人都在适应着或多或少,与家人的分离、与朋友的分离、与周围环境的分离。 还记得第...

  • SSTI模板注入

    前言 开局一张图,姿势全靠yy 模板引擎可以让(网站)程序实现界面与数据分离,业务代码与逻辑代码的分离,这大大提升...

  • 述说分离焦虑

    今天与大家聊一聊分离焦虑这件事。分离焦虑会出现在小朋友刚入幼儿园时,小朋友不习惯与父母分离,情绪上有些波动。分离焦...

网友评论

    本文标题:让titleview与pageview分离 标题与分页控件分离

    本文链接:https://www.haomeiwen.com/subject/eyakrftx.html