回到顶部,是比较常用的一个效果

核心代码

在ViewDidLoad中,添加回到顶部按钮

计算偏移量,如果当前显示的内容全部向上隐藏掉,则显示“回到顶部”按钮

//
// ViewController.m
// 回到顶部
//
// Created by Apple on 15/11/4.
// Copyright © 2015年 Apple. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) UIButton * topBtn;
@property (nonatomic, assign) CGFloat lastContentOffset;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:_tableView]; // 添加回到顶部按钮
_topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_topBtn.frame = CGRectMake(self.view.frame.size.width-, self.view.frame.size.height-, , );
[_topBtn setBackgroundImage:[UIImage imageNamed:@"nearby_return_top_btn"] forState:UIControlStateNormal];
[_topBtn addTarget:self action:@selector(backToTopButton) forControlEvents:UIControlEventTouchUpInside];
_topBtn.clipsToBounds = YES;
_topBtn.hidden = YES;
[self.view addSubview:_topBtn];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CMainCell = @"CMainCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CMainCell];
} cell.textLabel.text =[NSString stringWithFormat:@"测试广本%u",indexPath.row];
return cell;
} // MARK: 计算偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//MARK:列表滑动偏移量计算
CGPoint point = [self.tableView contentOffset]; if (point.y >= self.tableView.frame.size.height) {
self.topBtn.hidden = NO;
[self.view bringSubviewToFront:self.topBtn];
} else {
self.topBtn.hidden = YES;
}
} //MARK: 点击移动到顶部
- (void)backToTopButton{
[self.tableView setContentOffset:CGPointMake(, ) animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

效果:

源代码下载链接:http://pan.baidu.com/s/1c0c7t3E

最新文章

  1. 【python之路5】学习小结
  2. CSS换行文本溢出显示省略号,多行
  3. 参考例子,学习Func&lt;T, TResult&gt;委托
  4. [Spring MVC] - Interceptor 拦截器
  5. mq队列管理器命令
  6. ie支持CSS3标签
  7. C#中byte[]与string的转换
  8. zoj 3647 Gao the Grid
  9. 2014年度辛星全然解读html第七节
  10. shell编程——if语句
  11. Lucene.net 的性能探究--Lucene.net 的并发处理能力到底有多强?
  12. NodeJS二进制包安装和快捷键配置(适用于U盘版安装配置)
  13. 转载 --mysql函数大全
  14. Callcenter 模块解析
  15. Android之Fragment(碎片)方方面面
  16. Jsp+Struts2+JavaBean+DAO开发模式(1)
  17. luogu3305/bzoj3130 费用流 (二分答案+dinic)
  18. centos部署yapi爬坑记
  19. 大纲2.3 Internet
  20. iOS - UIMenuController

热门文章

  1. AIX文件系统和存储部署(转)
  2. 证书文件(pfx)读取时报 “指定的网络密码不正确”
  3. thymeleaf 标签的使用
  4. .NET Core开发日志——结构化日志
  5. [No0000C8]英特尔快速存储IRST要不要装
  6. 开始使用vue和vuetify
  7. 用CMD命令进行关机/重启
  8. shell 文件描述符
  9. [security][modsecurity] modsecurity 规则说明/中文/转发
  10. LeetCode 476 Number Complement 解题报告