制作目的
实现功能
- 实现一个可以自由添加到某个 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
效果展示