框架HGPageScrollView实现了类UICollectionView的功能,横向滑动浏览。分两个模式甲板模式和单页模式,能添加删除页面。
demo效果图:
甲板模式:
单页模式:
deckMode按钮:在两个模式间切换
add按钮:增加页面
remove按钮:删除页面
只能在甲板模式下才能实现增加,删除功能。
代码封装运用和UITableView一样
实现HGPageScrollView的datasource协议方法
自定义pageView类,相当于自定义cell
pageData对应pageView的model,给pageView填充上数据
#pragma mark - HGPageScrollView datasource
//设置page的数量
- (NSInteger)numberOfPagesInScrollView:(HGPageScrollView *)scrollView {
return _myPageDataArray.count;
}
//创建page
- (HGPageView *)pageScrollView:(HGPageScrollView *)scrollView viewForPageAtIndex:(NSInteger)index {
//page复用
static NSString *pageId = @"pageId";
MyPageView *pageView = (MyPageView*)[scrollView dequeueReusablePageWithIdentifier:pageId];
if (!pageView) {
pageView = [[[NSBundle mainBundle] loadNibNamed:@"MyPageView" owner:self options:nil] lastObject];
pageView.reuseIdentifier = pageId;
}
MyPageData *pageData = [_myPageDataArray objectAtIndex:index];
pageView.myPageData = pageData;
//设置page高度
CGRect frame = pageView.frame;
frame.size.height = 420;
pageView.frame = frame;
return pageView;
}
//设置HGPageScrollView的标题
- (NSString *)pageScrollView:(HGPageScrollView *)scrollView titleForPageAtIndex:(NSInteger)index;
{
MyPageData *pageData = [_myPageDataArray objectAtIndex:index];
return pageData.title;
}
//设置HGPageScrollView的子标题
- (NSString *)pageScrollView:(HGPageScrollView *)scrollView subtitleForPageAtIndex:(NSInteger)index;
{
MyPageData *pageData = [_myPageDataArray objectAtIndex:index];
return pageData.subtitle;
}
实现HGPageScrollView的delegate协议方法
当处于甲板模式,选中page后,切换到单页模式,重新布局page页面
#pragma mark - HGPageScrollView delegate
//选中page触发的协议方法
- (void)pageScrollView:(HGPageScrollView *)scrollView didSelectPageAtIndex:(NSInteger)index {
MyPageView *page = (MyPageView*)[scrollView pageAtIndex:index];
UIScrollView *pageContentsScrollView = (UIScrollView*)[page viewWithTag:10];
//重新布局page
UITextView *textView = (UITextView *)[page viewWithTag:3];
CGFloat margin = 12;
//计算文本高度
CGSize size = [textView.text sizeWithFont:textView.font
constrainedToSize:CGSizeMake(textView.frame.size.width, 2000)
lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = textView.frame;
frame.size.height = size.height + 4*margin;
textView.frame = frame;
pageContentsScrollView.contentSize = CGSizeMake(pageContentsScrollView.frame.size.width, frame.origin.y + frame.size.height);
pageContentsScrollView.scrollEnabled = YES;
}
pageData对应pageView的model,给pageView填充上数据