欢迎大家关注我的公众号,我会定期分享一些我在项目中遇到问题的解决办法和一些iOS实用的技巧,现阶段主要是整理出一些基础的知识记录下来
文章也会同步更新到我的博客:
ppsheep.com
一个简单的小功能,忘记在哪个APP上看到过,就做来玩一玩 实现起来还是很简单的
先看一下效果
分析一下我们需要实现的:
- UITableView 向上滑动时隐藏底部的按钮
- UITableView 向下滑动时,底部按钮出现
- UITableView 到达底部时,底部按钮也出现
创建工程啥的我都不讲了吧
在viewDidload中加上view
//加上这句 UIScrollView的Content就不会自动偏移64(导航栏和状态栏的高度)
self.automaticallyAdjustsScrollViewInsets = NO;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width, SCREEN.height-64) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
self.tableView = tableView;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(self.view.frame.size.width / 2 - 25, self.view.frame.size.height - 50, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:@"comments"] forState:UIControlStateNormal];
self.btn = btn;
[self.view addSubview:self.btn];
处理UIScrollView的滑动,一般都是在scrollViewDidScroll:方法中,这次也不例外
我们在这里监听当前scrollview的滑动距离 滑动方向等
#pragma mark - UITableView delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y > self.offsetY && scrollView.contentOffset.y > 0) {//向上滑动
//按钮消失
[UIView transitionWithView:self.btn duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.btn.frame = CGRectMake(SCREEN.width / 2 - 25, SCREEN.height, 50, 50);
} completion:NULL];
}else if (scrollView.contentOffset.y < self.offsetY ){//向下滑动
//按钮出现
[UIView transitionWithView:self.btn duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.btn.frame = CGRectMake(SCREEN.width / 2 - 25, SCREEN.height - 50, 50, 50);
} completion:NULL];
}
self.offsetY = scrollView.contentOffset.y;//将当前位移变成缓存位移
}
这样一个 特别简单的小功能 就实现了
源码放在了: