美文网首页
使用MJRefresh实现今日头条下拉刷新

使用MJRefresh实现今日头条下拉刷新

作者: AntKing | 来源:发表于2018-05-03 19:47 被阅读0次

    MJRefresh

    MJRefresh

    
     - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        __unsafe_unretained UITableView *tableView = self.tableView;
        
        // 下拉刷新
        tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            
            // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                
                [self afterLoadDataAnimation:tableView viewHight:30 title:@"想显示什么就填什么"];
            });
        }];
    }
    
    - (void)afterLoadDataAnimation:(UIScrollView*)scrollview viewHight:(CGFloat)height title:(NSString*)title{
        // 设置自动切换透明度(在导航栏下面自动隐藏)
        scrollview.mj_header.automaticallyChangeAlpha = YES;
        // 结束刷新
        NSLog(@"%f",scrollview.mj_offsetY);//默认值是-118
        NSLog(@"%f",scrollview.mj_header.mj_h);//默认是54
        [UIView animateWithDuration:0.25 animations:^{
            scrollview.mj_offsetY = -(64 + height);//注意适配iPhone X导航栏高度
        }completion:^(BOOL finished) {
            
             __block UILabel *lable;
             __block UIView *view;
            
            for (int i = 0; i < scrollview.mj_header.subviews.count; i++) {
                if (i == scrollview.mj_header.subviews.count - 2 && scrollview.mj_header.subviews[scrollview.mj_header.subviews.count - 2]) {
                    view = scrollview.mj_header.subviews[scrollview.mj_header.subviews.count - 2];
                }else if (i == scrollview.mj_header.subviews.count - 1 && scrollview.mj_header.subviews[scrollview.mj_header.subviews.count - 1]){
                    lable = scrollview.mj_header.subviews[scrollview.mj_header.subviews.count - 1];
                }
            }
            
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                view = [UIView new];
                lable = [UILabel new];
                
                view.backgroundColor = [UIColor redColor];
                view.layer.anchorPoint = CGPointMake(0.5, 0.5);
                view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2, scrollview.mj_header.mj_h - height, 1, height);
                
                
                lable.layer.anchorPoint = CGPointMake(0.5, 0.5);
                lable.frame = CGRectMake(0, scrollview.mj_header.mj_h - height, [UIScreen mainScreen].bounds.size.width, height);
                lable.textAlignment = NSTextAlignmentCenter;
                
                [scrollview.mj_header insertSubview:view atIndex:scrollview.mj_header.subviews.count];
                [scrollview.mj_header insertSubview:lable atIndex:scrollview.mj_header.subviews.count];
            });
            
            view.transform = CGAffineTransformIdentity;
            lable.text = title;
            [view setHidden:NO];
            [lable setHidden:NO];
            
            [UIView animateWithDuration:0.3 animations:^{
                view.transform = CGAffineTransformMakeScale([UIScreen mainScreen].bounds.size.width + 1, 1);
            } completion:^(BOOL finished) {
                
            }];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [scrollview.mj_header endRefreshing];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [view setHidden:YES];
                    [lable setHidden:YES];
                });
                
            });
        }];
    }
    

    相关文章

      网友评论

          本文标题:使用MJRefresh实现今日头条下拉刷新

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