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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

github:https://github.com/hellovoidworld/HVWWeibo

A.下拉刷新微博

1.需求

在“首页”界面,下拉到一定距离的时候刷新微博数据刷新数据的时候使用控件提示新数据要加在旧数据的前面刷新完毕隐藏刷新控件刷新数据完毕,导航栏下方弹出一个提示框,提示刷新微博数量

2.思路

直接使用系统自带的UIRefreshControl就可以做出动画效果使用微博的获取微博API参数since_id可以控制加载的微博从哪个id开始使用可变数组来拼接新旧微博数据创建一个UILabel放在导航控制器view上来提示刷新数据(放在TableView上会被滚走)

3.实现

(1)每条微博都有一个id,这个id是随时间递进叠加的,id越大越新

(2)获取微博API参数

(3)在“首页”控制器加上下拉刷新代码

以前的加载微博数据方法"loadWeiboData",也用刷新方法代替

1//HVWHomeViewController.m2- (void)viewDidLoad {3[super viewDidLoad];45self.tableView.delegate=self;67//设置导航栏8[self setupNavigationBar];910//添加刷新器11[self addRefresh];12}1314/** 初始化status*/15- (NSMutableArray *)statuses {16if(nil ==_statuses) {17_statuses =[NSMutableArray array];18}19return_statuses;20}2122/** 添加刷新器*/23- (void) addRefresh {24//下拉刷新最新微博25//添加刷新控件26UIRefreshControl *refreshControl =[[UIRefreshControl alloc] init];27self.refreshControl =refreshControl;28[self.view addSubview:refreshControl];2930//刷新控件下拉事件31[refreshControl addTarget:self action:@selector(refreshLatestWeibo:) forControlEvents:UIControlEventValueChanged];3233//开启的时候自动进入刷新状态34[refreshControl beginRefreshing];35//加载微博数据36[self refreshLatestWeibo:refreshControl];37}3839/** 刷新最新微博数据*/40- (void) refreshLatestWeibo:(UIRefreshControl *) refreshControl {41//把最新的微博数据加到原来的微博前面4243//创建AFNetworking的http操作中管理器44AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];4546//设置参数47NSMutableDictionary *param =[NSMutableDictionary dictionary];48param[@"access_token"] =[HVWAccountInfoTool accountInfo].access_token;4950/** 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。*/51HVWStatus *firstStatus =[self.statuses firstObject];52if(firstStatus) {53param[@"since_id"] =firstStatus.idstr;54}5556//发送请求57[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json"parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responSEObject) {58//HVWLog(@"获取微博数据成功-------%@", responseObject);5960//保存数据到内存61NSArray *dataArray = responseObject[@"statuses"];6263//得到新微博数据64//使用MJExtension直接进行字典-模型转换65NSArray *newStatus =[HVWStatus objectArrayWithKeyValuesArray:dataArray];6667//插入到微博数据数组的最前面68NSRange newWeiboRange = NSMakeRange(0, newStatus.count);69NSIndexSet *newWeiboIndexSet =[NSIndexSet indexSetWithIndexesInRange:newWeiboRange];70[self.statuses insertObjects:newStatus atIndexes:newWeiboIndexSet];7172//刷新数据73[self.tableView reloadData];74} failure:^(AFHTTPRequestOperation *operation, NSError *error) {75HVWLog(@"获取微博数据失败------%@", error);76}];7778//缩回刷新器79[refreshControl endRefreshing];80}

(4)添加一个UILabel提示更新微博数量,在请求微博数据成功后显示

1/** 弹出微博更新提示框*/2- (void) showRefreshIndicator:(int) refreshCount {3//创建UILabel4UILabel *refreshIndicatorLabel =[[UILabel alloc] init];5refreshIndicatorLabel.textAlignment =NSTextAlignmentCenter;67//设置文本8refreshIndicatorLabel.text = [NSString stringWithFormat:@"更新了%d条微博", refreshCount];910//设置背景11refreshIndicatorLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]];1213//设置位置尺寸14refreshIndicatorLabel.width =self.navigationController.view.width;15refreshIndicatorLabel.height =35;16refreshIndicatorLabel.x =0;17//因为一开始是藏在导航栏上的,所以要减去自身的高度18refreshIndicatorLabel.y = [UIapplicationsharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.height -refreshIndicatorLabel.height;1920//添加到导航控制器view,要加载导航器的下面21[self.navigationController.view insertSubview:refreshIndicatorLabel belowSubview:self.navigationController.navigationBar];2223//使用动画弹出24[UIView animateWithDuration:1.0animations:^{25//使用更改transform来实现26refreshIndicatorLabel.transform = CGAffineTransformMakeTranslation(0, refreshIndicatorLabel.height);27} completion:^(BOOL finished) {28//弹出完毕后,再使用动画缩回29[UIView animateWithDuration:1.0delay:1.0options:UIViewAnimationOptionCurveEaseInOut animations:^{30//恢复位置31refreshIndicatorLabel.transform =CGAffineTransformIdentity;32} completion:^(BOOL finished) {33//从导航view删除34[refreshIndicatorLabel removeFromSuperview];35}];36}];37}

B.上拉加载旧微博数据

1.需求

当浏览到了缓存的所有微博数据底部的时候,上拉加载更多的旧微博数据

也具有一个加载提示器

2.思路

跟下拉加载新微博一样,使用获取微博API中的“max_id”参数

如果需要加载等待动画图标(菊花图标),可以使用代码或xib创建一个UIView

加载在tableView的footerView上

3.实现

(1)设计一个“加载更多”控件

1//2//HVWLoadMoreWeiboFooterView.m3//HVWWeibo4//5//Created by hellovoidworld on 15/2/6.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"HVWLoadMoreWeiboFooterView.h"1011@interfaceHVWLoadMoreWeiboFooterView()1213/** 加载更多微博文本*/14@property(nonatomic, strong) UILabel *label;1516/** 加载中活动指示器*/17@property(nonatomic, strong) UIActivityIndicatorView *actIndicator;1819@end2021@implementationHVWLoadMoreWeiboFooterView2223-(instancetype)initWithFrame:(CGRect)frame {24self =[super initWithFrame:frame];2526self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]];2728//设置加载文本29UILabel *label =[[UILabel alloc] init];30label.textAlignment =NSTextAlignmentCenter;31label.text =@"上拉加载更多微博";32self.label =label;33[self addSubview:label];3435//设置加载活动指示器36//不同类型的活动指示器大小是不一样的,要注意37UIActivityIndicatorView *actIndicator =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];38self.actIndicator =actIndicator;39[self addSubview:actIndicator];4041returnself;42}4344/** 设置位置尺寸*/45- (void)layoutSubviews {46[super layoutSubviews];4748//设置本身frame49self.width =[UIScreen mainScreen].bounds.size.width;50self.height =35;5152//设置文本frame53self.label.frame =self.bounds;5455//设置活动指示器frame56CGFloat marginX =50;57self.actIndicator.x = self.width - self.actIndicator.width -marginX;58self.actIndicator.y = (self.height - self.actIndicator.height) *0.5;59}6061@end

加在tableView的footerView上,没有微博数据的时候不需要显示(app启动的时候)

1//HVWHomeViewController.m2/** 添加刷新器*/3- (void) addRefresh {4//下拉刷新最新微博5//添加刷新控件6UIRefreshControl *refreshControl =[[UIRefreshControl alloc] init];7self.refreshControl =refreshControl;8[self.view addSubview:refreshControl];910//刷新控件下拉事件11[refreshControl addTarget:self action:@selector(refreshLatestWeibo:) forControlEvents:UIControlEventValueChanged];1213//开启的时候自动进入刷新状态14[refreshControl beginRefreshing];15//加载微博数据16[self refreshLatestWeibo:refreshControl];1718//添加上拉刷新器19HVWLoadMoreWeiboFooterView *loadMoreFooter =[[HVWLoadMoreWeiboFooterView alloc] init];20self.loadMoreFooter =loadMoreFooter;21self.tableView.tableFooterView =loadMoreFooter;2223}2425- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {26//没有微博数据的时候,不需要显示“加载更多微博”控件27self.loadMoreFooter.hidden = self.statuses.count==0?YES:NO;2829returnself.statuses.count;30}

(2)上拉刷新动作监听

a.给“上拉刷新”控件增加状态管理属性和方法

1//2//HVWLoadMoreWeiboFooterView.h3//HVWWeibo4//5//Created by hellovoidworld on 15/2/6.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import<UIKit/UIKit.h>1011@interfaceHVWLoadMoreWeiboFooterView : UIView1213/** 是否正在刷新*/14@property(nonatomic, assign, getter=isRefreshing) BOOL refreshing;1516/** 开始刷新*/17- (void) beginRefresh;18/** 停止刷新*/19- (void) endRefresh;2021@end2223//24//HVWLoadMoreWeiboFooterView.m25//HVWWeibo26//27//Created by hellovoidworld on 15/2/6.28//Copyright (c) 2015年 hellovoidworld. All rights reserved.29//3031#import"HVWLoadMoreWeiboFooterView.h"3233@interfaceHVWLoadMoreWeiboFooterView()3435/** 加载更多微博文本*/36@property(nonatomic, strong) UILabel *label;3738/** 加载中活动指示器*/39@property(nonatomic, strong) UIActivityIndicatorView *actIndicator;4041@end4243@implementationHVWLoadMoreWeiboFooterView4445-(instancetype)initWithFrame:(CGRect)frame {46self =[super initWithFrame:frame];4748self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"timeline_new_status_background"]];4950//设置加载文本51UILabel *label =[[UILabel alloc] init];52label.textAlignment =NSTextAlignmentCenter;53label.text =@"上拉加载更多微博";54self.label =label;55[self addSubview:label];5657//设置加载活动指示器58//不同类型的活动指示器大小是不一样的,要注意59UIActivityIndicatorView *actIndicator =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];60self.actIndicator =actIndicator;61[self addSubview:actIndicator];6263returnself;64}6566/** 设置位置尺寸*/67- (void)layoutSubviews {68[super layoutSubviews];6970//设置本身frame71self.width =[UIScreen mainScreen].bounds.size.width;72self.height =35;7374//设置文本frame75self.label.frame =self.bounds;7677//设置活动指示器frame78CGFloat marginX =50;79self.actIndicator.x = self.width - self.actIndicator.width -marginX;80self.actIndicator.y = (self.height - self.actIndicator.height) *0.5;81}8283/** 开始刷新*/84- (void) beginRefresh {85self.label.text =@"正在努力加载更多微博...";86[self.actIndicator startAnimating];87self.refreshing =YES;88}8990/** 停止刷新*/91- (void) endRefresh {92self.label.text =@"上拉加载更多微博";93[self.actIndicator stopAnimating];94self.refreshing =NO;95}9697@end

b.在“首页”控制器中,监听滚动操作,当“上拉刷新”控件完全露出的时候启动刷新

1//HVWHomeViewController.m2/** 加载更多(旧)微博*/3- (void) loadMoreWeiboData {4//把更多的微博数据加到原来的微博后面56//创建AFNetworking的http操作中管理器7AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];89//设置参数10NSMutableDictionary *param =[NSMutableDictionary dictionary];11param[@"access_token"] =[HVWAccountInfoTool accountInfo].access_token;1213/** 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。*/14HVWStatus *lastStatus =[self.statuses lastObject];15if(lastStatus) {16param[@"max_id"] = @([lastStatus.idstr longLongValue] -1);17}1819//发送请求20[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json"parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {21//HVWLog(@"获取微博数据成功-------%@", responseObject);2223//保存数据到内存24NSArray *dataArray = responseObject[@"statuses"];2526//得到新微博数据27//使用MJExtension直接进行字典-模型转换28NSArray *newStatus =[HVWStatus objectArrayWithKeyValuesArray:dataArray];2930//插入到微博数据数组的后面31[self.statuses addObjectsFromArray:newStatus];3233//刷新数据34[self.tableView reloadData];35} failure:^(AFHTTPRequestOperation *operation, NSError *error) {36HVWLog(@"获取微博数据失败------%@", error);37}];3839[self.loadMoreFooter endRefresh];40}4142#pragmamark - UIScrollViewDelegate43- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {44//如果正在加载中,不用重复加载45if(self.loadMoreFooter.isRefreshing)return;4647//滚动时,scrollView处于屏幕顶部下方的内容长度48CGFloat scrollingDelta = scrollView.contentSize.height -scrollView.contentOffset.y;49//当scrollView向上滚栋到刚好露出“上拉刷新”控件时,scrollView处于屏幕下方的内容长度50CGFloat scrollViewHeighWithFooter = self.tableView.height - self.tabBarController.tabBar.height -self.loadMoreFooter.height;5152//当向上滚动至scrollView能够显示的内容少于刚好露出“上拉刷新”控件时显示的内容,证明“上拉刷新”控件已经完全露出,可以刷新53if(scrollingDelta <scrollViewHeighWithFooter) {54[self.loadMoreFooter beginRefresh];55[self loadMoreWeiboData];56}57}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航