#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;
}
// 設(shè)置布局樣式
- (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;
}
// 初始化左右滑動(dòng)視圖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;
}
// 注冊(cè)相關(guān)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;
}
// 設(shè)置頭部標(biāo)題欄點(diǎn)擊事件
- (void)customHeaderViewClock{
[self.headerView customHeaderCellClick:^(NSInteger index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionNone) animated:YES];
}];
}
// 設(shè)置子視圖
- (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];
}
// 設(shè)置子視圖
- (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);
}
}
// 設(shè)置cell點(diǎn)擊回調(diào)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;
}
// 設(shè)置子視圖
- (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添加數(shù)據(jù),就在這個(gè)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;
}
// 初始化上下滑動(dòng)tableView
- (UITableView *)tableView{
if (!_tableView) {
self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
[self registHelperCell];
}
return _tableView;
}
// 注冊(cè)cell
- (void)registHelperCell{
[self.tableView registerClass:[LRSlideTableCell class] forCellReuseIdentifier:LRSlideTableCellIdentifier];
}
// 設(shè)置子視圖
- (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 左右滑動(dòng)視圖效果實(shí)現(xiàn)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門颖对,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)捻撑,“玉大人,你說(shuō)我怎么就攤上這事」嘶迹” “怎么了番捂?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)描验。 經(jīng)常有香客問(wèn)我白嘁,道長(zhǎng),這世上最難降的妖魔是什么膘流? 我笑而不...
- 正文 為了忘掉前任絮缅,我火速辦了婚禮,結(jié)果婚禮上呼股,老公的妹妹穿的比我還像新娘耕魄。我一直安慰自己,他們只是感情好彭谁,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布吸奴。 她就那樣靜靜地躺著,像睡著了一般缠局。 火紅的嫁衣襯著肌膚如雪则奥。 梳的紋絲不亂的頭發(fā)上,一...
- 那天狭园,我揣著相機(jī)與錄音读处,去河邊找鬼。 笑死唱矛,一個(gè)胖子當(dāng)著我的面吹牛罚舱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绎谦,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼管闷,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了窃肠?” 一聲冷哼從身側(cè)響起包个,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铭拧,沒(méi)想到半個(gè)月后赃蛛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡搀菩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年呕臂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肪跋。...
- 正文 年R本政府宣布,位于F島的核電站阐虚,受9級(jí)特大地震影響序臂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜实束,卻給世界環(huán)境...
- 文/蒙蒙 一奥秆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咸灿,春花似錦构订、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至审胸,卻和暖如春亥宿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背砂沛。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像圆到,于是被迫代替她去往敵國(guó)和親怎抛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 首先富稻,很尷尬、失落的吐槽一下:本菜鳥(niǎo)不知道怎么上傳GIF,所以效果圖只能用圖片簡(jiǎn)單描述白胀。 先簡(jiǎn)單描述需求:1椭赋、最常...
- 越來(lái)越多的客戶端會(huì)遇到需要實(shí)現(xiàn)類似“頭條”客戶端那樣的多視圖頁(yè)面左右來(lái)回滑動(dòng)切換。那么今天我們就來(lái)封裝一下這個(gè)功能...
- 使用到的第三方庫(kù):iCarousel认境。 使用:iCarousel使用方式與UITableView相似胚委,具體看代碼。...
- 最近發(fā)現(xiàn)有很多App的tableView或者scrollView在滑動(dòng)過(guò)程中叉信,當(dāng)某視圖滑動(dòng)到頂部時(shí)亩冬,會(huì)懸停在頂部,...
- 1. 創(chuàng)建新的Android工程 新工程使用EmptyActivity. 打開(kāi)build.gradle 添加依賴 ...