支付宝首页模仿

前段时间接到个需求,简单而言就是模仿支付宝的首页,唯一的不同就是支付宝首页的主功能模块在屏幕上方,而需求中的主功能模块在屏幕下方。

页面分析

支付宝首页

第一轮分析

整个页面由三个模块组成:主功能模块、应用模块和信息模块。

  • 当页面由初始状态向上滑动时,应用模块和信息模块会同时向上滑动,而主功能模块的高度会随着滑动的距离而变化,大功能按钮逐渐消失,小功能按钮逐渐显示。


    向上滑动
  • 当小功能按钮完全显示以后,主功能模块的高度就会固定,而信息模块和应用模块还可以继续向上滑动。


    主功能模块高度固定
  • 当页面由初始状态下滑时,主功能模块和应用模块的位置都会固定不变,而信息模块则会展示刷新效果,并在刷新结束后恢复初始状态。
  • 不论是上滑还是下滑,信息模块右侧都会显示滚动条。


    向下滑动刷新

根据上述效果进行分析:

  1. 整个页面由两个UITableView控件组成:

    • 由于信息模块有下拉刷新功能并且右侧显示有滚动条,因此将信息模块视为tableView1。
    • 由于信息模块和应用模块可以一起滑动,而主功能模块仅仅是改变高度,因此信息模块和应用模块一起组成tableView2。
    • 由于主功能模块只是高度发生变化而位置并不变化,因此可以将主功能模块添加在主页面上,并且用与主功能模块一样大小的空白view充当tableView2的headerView。
  2. 初始状态下,由tableView2响应上滑事件,tableView1响应下滑事件。

  3. 一旦由tableView2响应了上滑事件,那么在恢复初始状态之前,下滑事件也会由tableView2来响应。

根据上述分析不难看出,整个页面效果实现的关键在于控制响应滑动事件的主体。暂且不论代码的复杂程度,其实现后的效果也不尽如人意。

第二轮分析

在第一轮分析的基础上,参考了一些网上的资料,进行了第二轮分析:

  1. 整个页面仅由一个UITableView控件组成,其主体就是信息模块。有一个小细节:当我们在主功能模块或应用模块上触摸下滑时,信息模块同样会进行下拉刷新。
  2. 主功能模块添加在主页面上,其高度随tableView滑动距离的变化而变化。
  3. 依次在tableView.headerView上添加应用模块和刷新模块。
  4. 主功能模块和应用模块正好覆盖了tableView.headerView。
    具体布局代码如下:
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - tabBarHeight) style:UITableViewStylePlain] ;
_tableView.delegate = self ;
_tableView.dataSource = self ;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
[self.view addSubview:_tableView] ;

UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, movedHeight + fixedNormalHeight)] ;
tableHeaderView.backgroundColor = [UIColor clearColor] ;
_tableView.tableHeaderView = tableHeaderView ;
    
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(movedHeight + fixedNormalHeight, 0, 0, 0) ;
    
_refreshView = [[UIView alloc]initWithFrame:CGRectMake(0, movedHeight + fixedNormalHeight - 50, [UIScreen mainScreen].bounds.size.width , 50)] ;
[_tableView.tableHeaderView addSubview:_refreshView] ;
    
_movedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, movedHeight + fixedNormalHeight)] ;
[_tableView addSubview:_movedView] ;
    
_fixedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixedNormalHeight)] ;
[self.view addSubview:_fixedView] ;

代码中,_movedView就是应用模块,_fixedView就是主功能模块。

  1. 滑动时:
    • 初始状态下,若tableView上滑,应用模块也会随之上滑;若tableView下滑,应用模块则会固定不动,而原本添加在tableView.headerView上的刷新动画就会显示出来。
    • 在恢复初始状态之前,若tableView先上滑再下滑,应用模块也会随之下滑。
      具体实现代码为:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat tableViewOffSetY = scrollView.contentOffset.y ;
    if(tableViewOffSetY >= 0) {
        _movedView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, movedHeight + fixedNormalHeight) ;
        if(tableViewOffSetY <= (fixedNormalHeight-fixedSmallHeight)) {
            CGRect frame = _fixedView.frame ;
            CGFloat height = fixedNormalHeight - tableViewOffSetY ;
            frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height) ;
            _fixedView.frame = frame ;   
        }
        else {
            _fixedView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixedSmallHeight) ;
        }
    }
    else {
        _movedView.frame = CGRectMake(0, tableViewOffSetY, [UIScreen mainScreen].bounds.size.width, movedHeight + fixedNormalHeight) ;
        _fixedView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixedNormalHeight) ;
        if(tableViewOffSetY<-refreshHeight-10 && [scrollView isDecelerating]) {
            [self requird] ;
            [self.tableView setContentOffset:CGPointMake(0, -refreshHeight) animated:YES] ;
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if(!decelerate) {
        if(scrollView.contentOffset.y >= 0) {
            if(scrollView.contentOffset.y < fixedSmallHeight*0.5) {
                [scrollView setContentOffset:CGPointMake(0, 0) animated:YES] ;
            }
            else if(scrollView.contentOffset.y < fixedSmallHeight) {
                [scrollView setContentOffset:CGPointMake(0, fixedSmallHeight) animated:YES] ;
            }
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(scrollView.contentOffset.y >= 0) {
        if(scrollView.contentOffset.y < fixedSmallHeight*0.5) {
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES] ;
        }
        else if(scrollView.contentOffset.y < fixedSmallHeight) {
            [scrollView setContentOffset:CGPointMake(0, fixedSmallHeight) animated:YES] ;
        }
    }
}

代码中,下拉超过一定距离就进行更新请求,并在请求期间显示请求模块

根据第二轮分析,整个页面的实现就会变得非常简单。

代码实现

具体的实现代码见:
https://github.com/bbbxxxbx/-

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,364评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,919评论 25 709
  • 雨是什么味道的 是橘子汽水般的甘甜 是青梅煮酒般的酸涩 还是 其他未曾谋面的 恰如 学生时代的我们 穿着傻气的校服...
    颜麦冬阅读 1,675评论 0 3
  • 一 前天晚上,和书生一起骑车出门转悠,回来路上他想买点水果。于是去了附近一家新开的大水果店。 晚上九点钟,街上人车...
    上官草字头阅读 4,346评论 0 1
  • 叶子是芳芳的闺蜜,徐强是芳芳的男友,有一次,徐强欠债被人追杀,可是芳芳把自己所有的积蓄都借给了叶子,因为叶子的母亲...
    落幕新城墙阅读 1,805评论 0 0

友情链接更多精彩内容