iOS 左右滑動(dòng)視圖效果實(shí)現(xiàn)

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;
}
// 設(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末作谚,一起剝皮案震驚了整個(gè)濱河市倒庵,隨后出現(xiàn)的幾起案子染坯,更是在濱河造成了極大的恐慌,老刑警劉巖俐镐,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異喧半,居然都是意外死亡食店,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門颖对,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)捻撑,“玉大人,你說(shuō)我怎么就攤上這事」嘶迹” “怎么了番捂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)描验。 經(jīng)常有香客問(wèn)我白嘁,道長(zhǎng),這世上最難降的妖魔是什么膘流? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任絮缅,我火速辦了婚禮,結(jié)果婚禮上呼股,老公的妹妹穿的比我還像新娘耕魄。我一直安慰自己,他們只是感情好彭谁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布吸奴。 她就那樣靜靜地躺著,像睡著了一般缠局。 火紅的嫁衣襯著肌膚如雪则奥。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天狭园,我揣著相機(jī)與錄音读处,去河邊找鬼。 笑死唱矛,一個(gè)胖子當(dāng)著我的面吹牛罚舱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绎谦,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼管闷,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了窃肠?” 一聲冷哼從身側(cè)響起包个,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铭拧,沒(méi)想到半個(gè)月后赃蛛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搀菩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年呕臂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肪跋。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡歧蒋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情谜洽,我是刑警寧澤萝映,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站阐虚,受9級(jí)特大地震影響序臂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜实束,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一奥秆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咸灿,春花似錦构订、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至审胸,卻和暖如春亥宿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背砂沛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工箩绍, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人尺上。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像圆到,于是被迫代替她去往敵國(guó)和親怎抛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容