制作目的
- 想要在特定的位置添加一個(gè)滾動(dòng)標(biāo)題欄
實(shí)現(xiàn)功能
- 實(shí)現(xiàn)一個(gè)可以自由添加到某個(gè) view 上的滾動(dòng)標(biāo)題欄,并與某個(gè)指定的 UICollectionView 進(jìn)行聯(lián)動(dòng)
使用方式
- pod 'LGFPageTitle' 或者 LGFPageTitleView
#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圖片添加, 每個(gè)title添加不同圖片效果展示, 少量title時(shí)title居中顯示
- (LGFPageTitleView *)oneTitleView {
if (!_oneTitleView) {
LGFPageTitleStyle *style = [LGFPageTitleStyle na];
// 固定寬度title 必要屬性 (如果值為titleview寬度 / title數(shù)組count,titleview將取消滾動(dòng))
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];
//-----如果添加不同圖片使用這個(gè)屬性---
style.un_select_image_names = self.oneTitleUnSelectImages;
style.select_image_names = self.oneTitleSelectImages;
style.title_image_bundel = LGFBundle;
//-----這些屬性直接決定展示哪邊圖片----
style.right_image_width = 20;
//-----這些屬性直接決定圖片相對(duì)于title位置----
style.right_image_spacing = 5.0;
//----------------------------------
//----少量title時(shí)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];
}
// 刷新數(shù)據(jù)源
[self.pageView reloadData];
// 刷新title數(shù)組
self.oneTitleView.style.titles = self.oneTitles;
[self.oneTitleView reloadAllTitlesSelectIndex:10];
}
#pragma mark - Collection View 數(shù)據(jù)源 和 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.childVCs.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
// cell請(qǐng)?jiān)O(shè)置與 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 {
// 在此代理方法中作子控制器首次加載數(shù)據(jù)的操作 防止多余未出現(xiàn)的控制器加載數(shù)據(jù)而導(dǎo)致的卡頓 (只對(duì)將要出現(xiàn)的cell上的子控制器作加載數(shù)據(jù)操作,其余只加載控制器)
ChildViewController *vc = self.childVCs[indexPath.item];
if (vc.datas.count == 0) {// 如果該子控制器數(shù)據(jù)源數(shù)組為空那么執(zhí)行請(qǐng)求數(shù)據(jù)
LGFLog(@"正在刷新第%ld頁", indexPath.row);
[vc loadData];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// titleview與pageview分離關(guān)聯(lián)關(guān)鍵方法,必須設(shè)置 (此方法作用:關(guān)聯(lián)titleview和外部pageview, 使其可以聯(lián)動(dòng))
[self.oneTitleView autoScrollTitle];
}
@end
效果展示

LGFPageTitle