iOS 左右滑动视图效果实现

2017-01-14 10:06:30来源:http://www.jianshu.com/p/ac3230c63431作者:LYSNote人点击

第七城市


Untitled.gif
#import "ViewController.h"
#import "LRSlideView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
LRSlideView *lrslideView = [[LRSlideView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) titleAry:@[@"表一",@"表二",@"表三",@"表四"]];
[self.view addSubview:lrslideView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface LRSlideView : UIView
- (instancetype)initWithFrame:(CGRect)frame titleAry:(NSArray *)titleAry;
@end
#import "LRSlideView.h"
#import "LRSlideCollectionCell.h"
#import "LRSlideHeaderView.h"
#define KWidth self.frame.size.width
#define KHeight self.frame.size.height
#define KHeaderHeight 30
@interface LRSlideView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)LRSlideHeaderView *headerView;
@property (nonatomic,strong)NSArray *titleAry;
@end
static NSString *identifier = @"UICollectionCell";
@implementation LRSlideView
- (instancetype)initWithFrame:(CGRect)frame titleAry:(NSArray *)titleAry{
if (self = [super initWithFrame:frame]) {
self.titleAry = [NSArray arrayWithArray:titleAry];
self.backgroundColor = [UIColor whiteColor];
[self setup];
}
return self;
}
// 设置布局样式
- (UICollectionViewFlowLayout *)flowLayout{
if (!_flowLayout) {
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.itemSize = CGSizeMake(KWidth, KHeight - KHeaderHeight);
_flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_flowLayout.minimumLineSpacing = 0;
_flowLayout.minimumLineSpacing = 0;
_flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
return _flowLayout;
}
// 初始化左右滑动视图collection
- (UICollectionView *)collectionView{
if (!_collectionView) {
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, KHeaderHeight, KWidth, KHeight - KHeaderHeight) collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.pagingEnabled = YES;
_collectionView.showsHorizontalScrollIndicator = NO;
[self registHelperCell];
}
return _collectionView;
}
// 注册相关cell
- (void)registHelperCell{
[self.collectionView registerClass:[LRSlideCollectionCell class] forCellWithReuseIdentifier:identifier];
}
// 初始化头视图
- (UIView *)headerView{
if (!_headerView) {
self.headerView = [[LRSlideHeaderView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeaderHeight) titleAry:self.titleAry];
[self customHeaderViewClock];
}
return _headerView;
}
// 设置头部标题栏点击事件
- (void)customHeaderViewClock{
[self.headerView customHeaderCellClick:^(NSInteger index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionNone) animated:YES];
}];
}
// 设置子视图
- (void)setup{
// 添加头视图
[self addSubview:self.headerView];
// 将collectionView添加在视图上
[self addSubview:self.collectionView];
}
#pragma mark - collectionView的代理方法 -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (!self.titleAry) {return 0;}
return self.titleAry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LRSlideCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
#import <UIKit/UIKit.h>
typedef void(^HeaderCellClick)(NSInteger index);
@interface LRSlideHeaderView : UIView
- (instancetype)initWithFrame:(CGRect)frame titleAry:(NSArray *)titleAry;
- (void)customHeaderCellClick:(HeaderCellClick)headerCellClick;
@end
#import "LRSlideHeaderView.h"
#import "LRSlideHeaderCell.h"
@interface LRSlideHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)NSArray *titleAry;
@property (nonatomic,copy)HeaderCellClick headerCellClick;
@end
static NSString *LRSlideHeaderCellIdentifier = @"LRSlideHeaderCell";
@implementation LRSlideHeaderView
- (instancetype)initWithFrame:(CGRect)frame titleAry:(NSArray *)titleAry
{
self = [super initWithFrame:frame];
if (self) {
self.titleAry = [NSArray arrayWithArray:titleAry];
[self setup];
}
return self;
}
// 初始化布局
- (UICollectionViewFlowLayout *)flowLayout{
if (!_flowLayout) {
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_flowLayout.itemSize = CGSizeMake(self.frame.size.width / self.titleAry.count, self.frame.size.height);
_flowLayout.minimumLineSpacing = 0;
_flowLayout.minimumLineSpacing = 0;
_flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
return _flowLayout;
}
// 初始化collectionView
- (UICollectionView *)collectionView{
if (!_collectionView) {
self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.showsHorizontalScrollIndicator = NO;
[self registHelperCell];
}
return _collectionView;
}
- (void)registHelperCell{
[self.collectionView registerClass:[LRSlideHeaderCell class] forCellWithReuseIdentifier:LRSlideHeaderCellIdentifier];
}
// 设置子视图
- (void)setup{
// 添加collectionView视图
[self addSubview:self.collectionView];
}
#pragma mark - collectionView的代理方法 -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.titleAry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LRSlideHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LRSlideHeaderCellIdentifier forIndexPath:indexPath];
cell.label.text = self.titleAry[indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.headerCellClick) {
self.headerCellClick(indexPath.row);
}
}
// 设置cell点击回调block
- (void)customHeaderCellClick:(HeaderCellClick)headerCellClick{
self.headerCellClick = headerCellClick;
}
@end
#import <UIKit/UIKit.h>
@interface LRSlideHeaderCell : UICollectionViewCell
@property (nonatomic,strong)UILabel *label;
@end
#import "LRSlideHeaderCell.h"
@implementation LRSlideHeaderCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
// 设置子视图
- (void)setup{
self.label = [[UILabel alloc] initWithFrame:self.contentView.bounds];
_label.textAlignment = NSTextAlignmentCenter;
_label.font = [UIFont systemFontOfSize:12];
_label.layer.borderWidth = 1;
_label.layer.borderColor = [UIColor lightGrayColor].CGColor;
[self.contentView addSubview:_label];
}
@end
#import <UIKit/UIKit.h>
@interface LRSlideCollectionCell : UICollectionViewCell
// 如果要是给tableView添加数据,就在这个cell里面做操作
@end
#import "LRSlideCollectionCell.h"
#import "LRSlideTableCell.h"
@interface LRSlideCollectionCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@end
static NSString *LRSlideTableCellIdentifier = @"LRSlideTableCellIdentifier";
@implementation LRSlideCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
// 初始化上下滑动tableView
- (UITableView *)tableView{
if (!_tableView) {
self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
[self registHelperCell];
}
return _tableView;
}
// 注册cell
- (void)registHelperCell{
[self.tableView registerClass:[LRSlideTableCell class] forCellReuseIdentifier:LRSlideTableCellIdentifier];
}
// 设置子视图
- (void)setup{
// 添加子视图
[self.contentView addSubview:self.tableView];
}
#pragma mark - tableView的代理方法 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LRSlideTableCell *cell = [tableView dequeueReusableCellWithIdentifier:LRSlideTableCellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"11111111111";
return cell;
}
@end
#import "LRSlideTableCell.h"
@implementation LRSlideTableCell
@end
#import <UIKit/UIKit.h>
@interface LRSlideTableCell : UITableViewCell
@end



第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台