#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
iOS 左右滑动视图效果实现
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 首先,很尴尬、失落的吐槽一下:本菜鸟不知道怎么上传GIF,所以效果图只能用图片简单描述。 先简单描述需求:1、最常...
- 越来越多的客户端会遇到需要实现类似“头条”客户端那样的多视图页面左右来回滑动切换。那么今天我们就来封装一下这个功能...
- 使用到的第三方库:iCarousel。 使用:iCarousel使用方式与UITableView相似,具体看代码。...
- 最近发现有很多App的tableView或者scrollView在滑动过程中,当某视图滑动到顶部时,会悬停在顶部,...
- 1. 创建新的Android工程 新工程使用EmptyActivity. 打开build.gradle 添加依赖 ...