美文网首页
微信下拉历史消息

微信下拉历史消息

作者: zhlv | 来源:发表于2018-04-23 22:08 被阅读0次

直接上代码吧!!!

要不先看一下效果

//

//  LZView.h

//  WeChatPulldownMessage

//

//  Created by vv-lvzhao on 2018/4/22.

//  Copyright © 2018年 lvzhao. All rights reserved.

//

#import

#define K_HeadViewHeight44

///屏幕的宽

#define K_SCREENWIDTH [UIScreen mainScreen].bounds.size.width

///屏幕的高

#define K_SCREENHEIGHT  [UIScreen mainScreen].bounds.size.height

///导航栏的高度

#define K_NAVHEIGHT  ((K_SCREENHEIGHT ==812)?88:64)

///tabbar的高度

#define K_BARHEIGHT  ((K_SCREENHEIGHT ==812) ?83:49)

//判断设备是否是iPhoneX!!

#define K_Device_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125,2436), [[UIScreen mainScreen] currentMode].size) : NO)

@interface LZView : UIView

@end

//

//  LZView.m

//  WeChatPulldownMessage

//

//  Created by vv-lvzhao on 2018/4/22.

//  Copyright © 2018年 lvzhao. All rights reserved.

//

#import "LZView.h"

@interface LZView()

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) UIView * headView; //headView loading

@property (nonatomic,assign) BOOL hasPrePage; //是否还有历史消息

@property (nonatomic,assign) BOOL isPulldownLoading; //开始加载历史消息

@property(nonatomic,assign)BOOLisEenDraggingAnimation;//加载历史消息动画

@property (nonatomic,strong) NSMutableArray *dataArray; //数据源

@end

@implementation LZView

- (instancetype)init

{

    self= [superinit];

    if(self) {

        self.backgroundColor = [UIColor redColor];

        [selfsetupView];

    }

    return self;

}

//创建UI

- (void)setupView{

    [self addSubview:self.tableView];

    self.tableView.tableHeaderView = self.headView;

    self.dataArray = [[NSMutableArray alloc]init];

    for(inti =0; i <15; i++){

        [self.dataArrayaddObject:@"测试信息"];

    }

    self.hasPrePage = YES;

    [self.tableView reloadData];

    dispatch_async(dispatch_get_main_queue(), ^{

        if(self.dataArray.count>0){

            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count -1 inSection:0]  atScrollPosition:UITableViewScrollPositionBottom animated:NO];

        }else{

            self.hasPrePage=NO;

            self.tableView.tableHeaderView=nil;

        }    });

}

#pragma mark -lanjiazai

- (UITableView*)tableView{

    if(!_tableView){

        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH,

                                                                      (K_Device_Is_iPhoneX) ?K_SCREENHEIGHT-34-K_NAVHEIGHT:

                                                                      K_SCREENHEIGHT  -K_NAVHEIGHT)style:UITableViewStylePlain];

        _tableView.delegate=self;

        _tableView.dataSource=self;

        _tableView.estimatedRowHeight = 0;

        _tableView.estimatedSectionFooterHeight = 0;

        _tableView.estimatedSectionHeaderHeight = 0;

        _tableView.backgroundColor = [UIColor yellowColor];

        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    }

    return _tableView;

}

- (UIView*)headView{

    if(!_headView){

        _headView= [[UIViewalloc]init];

        _headView.backgroundColor = [UIColor clearColor];

        _headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, K_HeadViewHeight);

        // loading

        UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        loading.center=CGPointMake(K_SCREENWIDTH*0.5,K_HeadViewHeight*0.5);

        [loadingstartAnimating];

        [_headViewaddSubview:loading];

    }

    return _headView;

}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    cell.textLabel.text=self.dataArray[indexPath.row];

    returncell;

}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 50;

}

#pragma mark - UIScrollViewDelegate

//只要滚动了就会触发

- (void)scrollViewDidScroll:(UIScrollView*)scrollView{

    NSLog(@"偏移量%f",scrollView.contentOffset.y);

    if (self.isPulldownLoading && self.hasPrePage) {

        self.tableView.tableHeaderView = self.headView;

        self.isPulldownLoading = NO;

    }

}

//开始拖拽视图

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView;

{

    self.isPulldownLoading = YES;

}

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{

    if(scrollView.contentOffset.y <= .0f && !self.isPulldownLoading&&!self.isEenDraggingAnimation){

        self.isEenDraggingAnimation = YES;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            if(scrollView.contentOffset.y<=K_HeadViewHeight){

                [selfpulldownLoad];

            }

            self.isEenDraggingAnimation = NO;

        });

    }

}

#pragma mark - 下拉历史消息

- (void)pulldownLoad

{

    if(self.isPulldownLoading)return;

    self.isPulldownLoading = YES;

    //获取历史消息

    NSArray* histortMessages = [selfloadHistoryMessage];

    self.tableView.tableHeaderView = nil;

    self.isPulldownLoading = NO;

    if([histortMessagescount] <15) {

        self.hasPrePage=NO;

        self.tableView.tableHeaderView = nil;

    }

    //插入消息

    [selfinsertOldMessages:histortMessages];

}

//加载历史消息

- (void)insertOldMessages:(NSArray*)oldMessages  {

    if([oldMessagescount] >0&&self.tableView.contentOffset.y<=10.0f){

        @synchronized(self){

            CGFloatoffsetOfButtom =self.tableView.contentSize.height-self.tableView.contentOffset.y;

            [self.dataArray insertObjects:oldMessages atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[oldMessages count])]];

            [self.tableViewreloadData];

#warning 修改这里的方法就OK了

            self.tableView.contentOffset=CGPointMake(0.0f,self.tableView.contentSize.height- offsetOfButtom -K_HeadViewHeight

                                                    );

        }

    }

}

#pragma makr - 获取历史消息

- (NSArray*)loadHistoryMessage{

    NSMutableArray * array = [[NSMutableArray alloc]init];

    for(inti =0; i <15; i++){

        [arrayaddObject:@"测试"];

    }

    returnarray;

}

@end

注意设置这个    self.navigationController.navigationBar.translucent = NO;

要不就修改 "insertOldMessages" 这个函数

如果你觉得可以,直接下载代码看一下

Demo

相关文章

  • 微信下拉历史消息

    直接上代码吧!!! 要不先看一下效果 // // LZView.h // WeChatPulldownMessag...

  • 仿微信消息下拉刷新加载样式

    先解释一下这个需求,因为我们项目的用的MJ第三方做刷新的,但是MJ刷新时,刷新完屏幕会跳一下,让人很不爽,所以想要...

  • 微信电脑端怎样查看公众号历史消息?

    微信电脑端怎样查看公众号历史消息? 微信电脑端改版后,怎样查看公众号历史消息呢?(以百度百家号为例) 方法1: 1...

  • 小程序开发 2018新商机 创业者首选 !

    现在每天打开微信,都会看到微信群里有很多小程序推广,而且只要你点进去,就会在微信的下拉栏里保存,所以不知不觉的下拉...

  • 打造Android微信朋友圈下拉刷新控件

    微信朋友圈我们都经常用,朋友圈的下拉刷新比较有意思,我们今天将要模仿打造微信朋友圈的下拉刷新控件,当然微信的这种刷...

  • 微信下拉露底

    在最外层包一层,设置属性为fixed,里面需要滚动的内容用一下scroll插件。即可解决。

  • 微信小程序学习记录

    1.下拉刷新,上拉加载动画效果wx.showNavigationBarLoading(); 微信小程序只有下拉刷新...

  • 如何查询物流?(每日一淘)

    微信聊天主页下拉微信,出现【每日一淘精选】小程序,按图指引操作就好。

  • 扒一扒立信微生活的表白墙

    一、介绍 一直想爬微信公众号的历史信息,无奈限于腾讯爸爸的微信反爬机制——PC端历史消息只能看到10条,除了通过中...

  • 微信消息

    微信关注群好几个,每天都是大量的群消息;公众号也关注不少,好多都是 日更,还好几篇;我还有听书习惯;时间不够...

网友评论

      本文标题:微信下拉历史消息

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