仿半糖首頁效果

最近公司APP更新版本,順帶更新了整個(gè)首頁的效果,花了幾天時(shí)間整理,也看了網(wǎng)上很多“仿半糖首頁效果”的demo,但是發(fā)現(xiàn)有一個(gè)共同的問題是:用戶只能作用于tableview或者scrollview上才能滑動(dòng),如果頁面上布局的控件很多,導(dǎo)致下面能夠滑動(dòng)并且展示數(shù)據(jù)的tableview顯示在屏幕底部,這樣對(duì)于用戶的體驗(yàn)性就很差,用戶只能手指移動(dòng)底部的tableview或者scrollview來使上面拍版的控件向上移動(dòng)。下面說說我的做法:

下面是我構(gòu)建的一個(gè)布局

布局

整個(gè)其實(shí)是個(gè)uitableview,其中把滾動(dòng)標(biāo)簽作為存放滾動(dòng)列表section的頭部視圖,除此之外另外的控件可以放倒tableview的別的section中,主要的是存放滾動(dòng)切換列表section,必須是容器表格最后一個(gè)section。其實(shí)主要是cell里面嵌套scrollview,scrollview嵌套多個(gè)表格,這樣問題就來了,同向滑動(dòng)的時(shí)候,我們?cè)趺粗阑瑒?dòng)的是哪個(gè)scrollview。

那我們一步一步代碼解釋:

首先主視圖構(gòu)建一個(gè)tableview容器,不多說大家都會(huì):

其中又一點(diǎn),這個(gè)主容器tableview必須要支持多手勢(shì),

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

構(gòu)建底部的滑動(dòng)表格

#import@interface tableviewViewController : UIViewController

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) BOOL vcCanScroll;

@end

@implementation tableviewViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];

_tableView.delegate = self;

_tableView.dataSource = self;

//? ? _tableView.bounces = NO;

[self.view addSubview:_tableView];

}

#pragma mark UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 20;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 80;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

return cell;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

if (!self.vcCanScroll) {

scrollView.contentOffset = CGPointZero;

}

if (scrollView.contentOffset.y <= 0) {

self.vcCanScroll = NO;

scrollView.contentOffset = CGPointZero;

[[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil];//到頂通知父視圖改變狀態(tài)

}

self.tableView.showsVerticalScrollIndicator = _vcCanScroll?YES:NO;

}

其實(shí)主要一點(diǎn)是監(jiān)聽scrollview滑動(dòng)的位置。

在主視圖監(jiān)聽子tableview發(fā)送給俯視圖改變狀態(tài)的通知

[KNotificationCenter addObserver:self selector:@selector(getOffset) name:@"leaveTop" object:nil];

- (void)getOffset

{

self.canScroll = YES;

tableviewViewController *vc = (tableviewViewController *)[tableViewArray objectAtIndex:self.selectIndex];

vc.vcCanScroll = NO;

}

這里也是需要監(jiān)聽主scrollview的滑動(dòng)位置

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGFloat bottomCellOffset = [mytableview rectForSection:1].origin.y;

if (scrollView.contentOffset.y >= bottomCellOffset) {

scrollView.contentOffset = CGPointMake(0, bottomCellOffset);

tableviewViewController *vc = (tableviewViewController *)[tableViewArray objectAtIndex:self.selectIndex];

if (self.canScroll) {

self.canScroll = NO;

vc.vcCanScroll = YES;

}

}else{

if (!self.canScroll) {//子視圖沒到頂部

scrollView.contentOffset = CGPointMake(0, bottomCellOffset);

}

mytableview.showsVerticalScrollIndicator = self.canScroll;

}


其實(shí)寫到這里了 主要功能就完成了 。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容