[iOS微博项目 - 3.0] - 手动刷新微博

时间:2021-12-04 14:07:31
A.下拉刷新微博
1.需求
  • 在“首页”界面,下拉到一定距离的时候刷新微博数据
  • 刷新数据的时候使用控件提示
  • 新数据要加在旧数据的前面
  • 刷新完毕隐藏刷新控件
  • 刷新数据完毕,导航栏下方弹出一个提示框,提示刷新微博数量
2.思路
  • 直接使用系统自带的UIRefreshControl就可以做出动画效果
  • 使用微博的获取微博API参数since_id可以控制加载的微博从哪个id开始
  • 使用可变数组来拼接新旧微博数据
  • 创建一个UILabel放在导航控制器view上来提示刷新数据(放在TableView上会被滚走)
3.实现
(1)每条微博都有一个id,这个id是随时间递进叠加的,id越大越新
[iOS微博项目 - 3.0] - 手动刷新微博
(2)获取微博API参数
[iOS微博项目 - 3.0] - 手动刷新微博
(3)在“首页”控制器加上下拉刷新代码
以前的加载微博数据方法"loadWeiboData",也用刷新方法代替
 //  HVWHomeViewController.m
- (void)viewDidLoad {
[super viewDidLoad]; self.tableView.delegate = self; // 设置导航栏
[self setupNavigationBar]; // 添加刷新器
[self addRefresh];
} /** 初始化status */
- (NSMutableArray *)statuses {
if (nil == _statuses) {
_statuses = [NSMutableArray array];
}
return _statuses;
} /** 添加刷新器 */
- (void) addRefresh {
// 下拉刷新最新微博
// 添加刷新控件
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = refreshControl;
[self.view addSubview:refreshControl]; // 刷新控件下拉事件
[refreshControl addTarget:self action:@selector(refreshLatestWeibo:) forControlEvents:UIControlEventValueChanged]; // 开启的时候自动进入刷新状态
[refreshControl beginRefreshing];
// 加载微博数据
[self refreshLatestWeibo:refreshControl];
} /** 刷新最新微博数据 */
- (void) refreshLatestWeibo:(UIRefreshControl *) refreshControl {
// 把最新的微博数据加到原来的微博前面 // 创建AFNetworking的http操作中管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token; /** 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。*/
HVWStatus *firstStatus = [self.statuses firstObject];
if (firstStatus) {
param[@"since_id"] = firstStatus.idstr;
} // 发送请求
[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// HVWLog(@"获取微博数据成功-------%@", responseObject); // 保存数据到内存
NSArray *dataArray = responseObject[@"statuses"]; // 得到新微博数据
// 使用MJExtension直接进行字典-模型转换
NSArray *newStatus = [HVWStatus objectArrayWithKeyValuesArray:dataArray]; // 插入到微博数据数组的最前面
NSRange newWeiboRange = NSMakeRange(, newStatus.count);
NSIndexSet *newWeiboIndexSet = [NSIndexSet indexSetWithIndexesInRange:newWeiboRange];
[self.statuses insertObjects:newStatus atIndexes:newWeiboIndexSet]; // 刷新数据
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HVWLog(@"获取微博数据失败------%@", error);
}]; // 缩回刷新器
[refreshControl endRefreshing];
}
[iOS微博项目 - 3.0] - 手动刷新微博
(4)添加一个UILabel提示更新微博数量,在请求微博数据成功后显示
 /** 弹出微博更新提示框 */
- (void) showRefreshIndicator:(int) refreshCount {
// 创建UILabel
UILabel *refreshIndicatorLabel = [[UILabel alloc] init];
refreshIndicatorLabel.textAlignment = NSTextAlignmentCenter; // 设置文本
refreshIndicatorLabel.text = [NSString stringWithFormat:@"更新了%d条微博", refreshCount]; // 设置背景
refreshIndicatorLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]]; // 设置位置尺寸
refreshIndicatorLabel.width = self.navigationController.view.width;
refreshIndicatorLabel.height = ;
refreshIndicatorLabel.x = ;
// 因为一开始是藏在导航栏上的,所以要减去自身的高度
refreshIndicatorLabel.y = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.height - refreshIndicatorLabel.height; // 添加到导航控制器view,要加载导航器的下面
[self.navigationController.view insertSubview:refreshIndicatorLabel belowSubview:self.navigationController.navigationBar]; // 使用动画弹出
[UIView animateWithDuration:1.0 animations:^{
// 使用更改transform来实现
refreshIndicatorLabel.transform = CGAffineTransformMakeTranslation(, refreshIndicatorLabel.height);
} completion:^(BOOL finished) {
// 弹出完毕后,再使用动画缩回
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// 恢复位置
refreshIndicatorLabel.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
// 从导航view删除
[refreshIndicatorLabel removeFromSuperview];
}];
}];
}
[iOS微博项目 - 3.0] - 手动刷新微博
B.上拉加载旧微博数据
1.需求
当浏览到了缓存的所有微博数据底部的时候,上拉加载更多的旧微博数据
也具有一个加载提示器
2.思路
跟下拉加载新微博一样,使用获取微博API中的“max_id”参数
如果需要加载等待动画图标(菊花图标),可以使用代码或xib创建一个UIView
加载在tableView的footerView上
[iOS微博项目 - 3.0] - 手动刷新微博
3.实现
(1)设计一个“加载更多”控件
 //
// HVWLoadMoreWeiboFooterView.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/6.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLoadMoreWeiboFooterView.h" @interface HVWLoadMoreWeiboFooterView() /** 加载更多微博文本 */
@property(nonatomic, strong) UILabel *label; /** 加载中活动指示器 */
@property(nonatomic, strong) UIActivityIndicatorView *actIndicator; @end @implementation HVWLoadMoreWeiboFooterView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]]; // 设置加载文本
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"上拉加载更多微博";
self.label = label;
[self addSubview:label]; // 设置加载活动指示器
// 不同类型的活动指示器大小是不一样的,要注意
UIActivityIndicatorView *actIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.actIndicator = actIndicator;
[self addSubview:actIndicator]; return self;
} /** 设置位置尺寸 */
- (void)layoutSubviews {
[super layoutSubviews]; // 设置本身frame
self.width = [UIScreen mainScreen].bounds.size.width;
self.height = ; // 设置文本frame
self.label.frame = self.bounds; // 设置活动指示器frame
CGFloat marginX = ;
self.actIndicator.x = self.width - self.actIndicator.width - marginX;
self.actIndicator.y = (self.height - self.actIndicator.height) * 0.5;
} @end
加在tableView的footerView上,没有微博数据的时候不需要显示(app启动的时候)
 //  HVWHomeViewController.m
/** 添加刷新器 */
- (void) addRefresh {
// 下拉刷新最新微博
// 添加刷新控件
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = refreshControl;
[self.view addSubview:refreshControl]; // 刷新控件下拉事件
[refreshControl addTarget:self action:@selector(refreshLatestWeibo:) forControlEvents:UIControlEventValueChanged]; // 开启的时候自动进入刷新状态
[refreshControl beginRefreshing];
// 加载微博数据
[self refreshLatestWeibo:refreshControl]; // 添加上拉刷新器
HVWLoadMoreWeiboFooterView *loadMoreFooter = [[HVWLoadMoreWeiboFooterView alloc] init];
self.loadMoreFooter = loadMoreFooter;
self.tableView.tableFooterView = loadMoreFooter; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 没有微博数据的时候,不需要显示“加载更多微博”控件
self.loadMoreFooter.hidden = self.statuses.count==?YES:NO; return self.statuses.count;
}
[iOS微博项目 - 3.0] - 手动刷新微博
(2)上拉刷新动作监听
a.给“上拉刷新”控件增加状态管理属性和方法
 //
// HVWLoadMoreWeiboFooterView.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/6.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @interface HVWLoadMoreWeiboFooterView : UIView /** 是否正在刷新 */
@property(nonatomic, assign, getter=isRefreshing) BOOL refreshing; /** 开始刷新 */
- (void) beginRefresh;
/** 停止刷新 */
- (void) endRefresh; @end //
// HVWLoadMoreWeiboFooterView.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/6.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLoadMoreWeiboFooterView.h" @interface HVWLoadMoreWeiboFooterView() /** 加载更多微博文本 */
@property(nonatomic, strong) UILabel *label; /** 加载中活动指示器 */
@property(nonatomic, strong) UIActivityIndicatorView *actIndicator; @end @implementation HVWLoadMoreWeiboFooterView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]]; // 设置加载文本
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"上拉加载更多微博";
self.label = label;
[self addSubview:label]; // 设置加载活动指示器
// 不同类型的活动指示器大小是不一样的,要注意
UIActivityIndicatorView *actIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.actIndicator = actIndicator;
[self addSubview:actIndicator]; return self;
} /** 设置位置尺寸 */
- (void)layoutSubviews {
[super layoutSubviews]; // 设置本身frame
self.width = [UIScreen mainScreen].bounds.size.width;
self.height = ; // 设置文本frame
self.label.frame = self.bounds; // 设置活动指示器frame
CGFloat marginX = ;
self.actIndicator.x = self.width - self.actIndicator.width - marginX;
self.actIndicator.y = (self.height - self.actIndicator.height) * 0.5;
} /** 开始刷新 */
- (void) beginRefresh {
self.label.text = @"正在努力加载更多微博...";
[self.actIndicator startAnimating];
self.refreshing = YES;
} /** 停止刷新 */
- (void) endRefresh {
self.label.text = @"上拉加载更多微博";
[self.actIndicator stopAnimating];
self.refreshing = NO;
} @end
b.在“首页”控制器中,监听滚动操作,当“上拉刷新”控件完全露出的时候启动刷新
 //  HVWHomeViewController.m
/** 加载更多(旧)微博 */
- (void) loadMoreWeiboData {
// 把更多的微博数据加到原来的微博后面 // 创建AFNetworking的http操作中管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token; /** 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。*/
HVWStatus *lastStatus = [self.statuses lastObject];
if (lastStatus) {
param[@"max_id"] = @([lastStatus.idstr longLongValue] - );
} // 发送请求
[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// HVWLog(@"获取微博数据成功-------%@", responseObject); // 保存数据到内存
NSArray *dataArray = responseObject[@"statuses"]; // 得到新微博数据
// 使用MJExtension直接进行字典-模型转换
NSArray *newStatus = [HVWStatus objectArrayWithKeyValuesArray:dataArray]; // 插入到微博数据数组的后面
[self.statuses addObjectsFromArray:newStatus]; // 刷新数据
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HVWLog(@"获取微博数据失败------%@", error);
}]; [self.loadMoreFooter endRefresh];
} #pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// 如果正在加载中,不用重复加载
if (self.loadMoreFooter.isRefreshing) return; // 滚动时,scrollView处于屏幕顶部下方的内容长度
CGFloat scrollingDelta = scrollView.contentSize.height - scrollView.contentOffset.y;
// 当scrollView向上滚栋到刚好露出“上拉刷新”控件时,scrollView处于屏幕下方的内容长度
CGFloat scrollViewHeighWithFooter = self.tableView.height - self.tabBarController.tabBar.height - self.loadMoreFooter.height; // 当向上滚动至scrollView能够显示的内容少于刚好露出“上拉刷新”控件时显示的内容,证明“上拉刷新”控件已经完全露出,可以刷新
if (scrollingDelta < scrollViewHeighWithFooter) {
[self.loadMoreFooter beginRefresh];
[self loadMoreWeiboData];
}
}
[iOS微博项目 - 3.0] - 手动刷新微博