iOS 滚动到顶部~demo

     联系人:石虎 QQ:1224614774  昵称: 嗡嘛呢叭咪哄

                        QQ群:807236138  群称: iOS 技术交流学习群

                       QQ群:713799633   群称: iOS技术交流学习群-2

一、概念

 

 1.此方法很简单,有操作视图和方法,动画效果参考

  2. 图片下面有

  3.GIF 图请看简书:  http://www.jianshu.com/p/9b48b91c3dd6

 

二、代码实现

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

//全局tableView
@property (nonatomic,strong)UITableView *tableView;
//全局滚动按钮
@property (nonatomic,strong)UIButton *btnTop;
@end


@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];

    //添加tableView
    [self.viewaddSubview:self.tableView];

    //添加滚动按钮
    [selfscrollTopView];
}

#pragma mark - 设置tableView
- (UITableView *)tableView{

    if (!_tableView) {

        _tableView = [[UITableViewalloc]init];
        _tableView.frame =CGRectMake(0,64,self.view.frame.size.width , self.view.frame.size.height);
        _tableView.backgroundColor = [UIColorlightGrayColor];
        _tableView.delegate =self;
        _tableView.dataSource =self;
        _tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;
    }
    return_tableView;
}

#pragma mark - tableView 数据源-
//共多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 100;
}

//每行显示的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //创建标识符
    staticNSString *cellId =@"cellid";
    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

    //缓存池
    if (!cell) {
        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellId];
    }

    //标题
    cell.textLabel.text = [NSStringstringWithFormat:@"第 %ld个",(long)indexPath.row];
    return cell;
}

//创建滚动顶部按钮
- (void)scrollTopView{

    _btnTop= [[UIButtonalloc]init];
    _btnTop.frame =CGRectMake(self.view.bounds.size.width -54,self.view.bounds.size.height -98, 44, 44);

    //添加图片
    [_btnTopsetImage:[UIImageimageNamed:@"icon_up"]forState:UIControlStateNormal];
    //设置先隐藏
    _btnTop.hidden =YES;
    //设置背景颜色方便测试
    [_btnTopsetBackgroundColor:[UIColororangeColor]];
    //监听
    [_btnTopaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];

    //添加视图
    [self.viewaddSubview:_btnTop];

}

#pragma mark
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //动态计算高度
    CGFloat gap =self.tableView.contentOffset.y -  scrollView.frame.size.height *1.5;
    if (gap <0) {
        //设置小于0隐藏
        self.btnTop.hidden =YES;
    } else {
        //设置大于0显示
        self.btnTop.hidden =NO;
    }
}

- (void)btnClick{

 //回到顶部
 [self.tableViewsetContentOffset:CGPointZero];
}

@end

谢谢!!!

 

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值