美文网首页
ios10UIScrollView和它的子类的下拉刷新

ios10UIScrollView和它的子类的下拉刷新

作者: zhouios | 来源:发表于2016-09-23 10:17 被阅读0次

在iOS10以前,只有UITableView有苹果自带下拉刷新;现在苹果在iOS10推出了UIScrollView和它的子类的下拉刷新,它们的下拉刷新和以前的UITableView下拉刷新几乎是一样的,如下所示:

1,建一个控制器,在viewDidLoad方法中,添加UIScrollView和下拉刷新UIRefreshControl;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //在view上添加UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //这是一个全局的scrollView变量
    self.scrollView = scrollView;
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    
    //在UIScrollView上添加一个红色的子view
    UIView *redView = [[UIView alloc] init];
    redView.frame = CGRectMake(0, 0, scrollView.bounds.size.width, 800);
    redView.backgroundColor = [UIColor redColor];
    [scrollView addSubview:redView];
    
    //设置UIScrollView的contentSize(滚动范围)
    scrollView.contentSize = CGSizeMake(0, redView.bounds.size.height);
    
    //添加下拉刷新
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    scrollView.refreshControl = refreshControl;
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    
}
2,执行下拉刷新的方法:
- (void)refresh:(UIRefreshControl *)refreshes
{
    if (refreshes.refreshing) {
        
        refreshes.attributedTitle = [[NSAttributedString alloc] initWithString:@"刷新"];
        //延迟2后执行
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [refreshes endRefreshing];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                refreshes.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
            });
        });  
    }
}

相关文章

网友评论

      本文标题:ios10UIScrollView和它的子类的下拉刷新

      本文链接:https://www.haomeiwen.com/subject/frdtyttx.html