微信下拉历史消息
直接上代码吧!!!
要不先看一下效果
![](https://img.haomeiwen.com/i4267049/37008df7c832ad07.gif)
//
// 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" 这个函数
如果你觉得可以,直接下载代码看一下