[iOS微博项目 - 3.0] - 手动刷新微博
- 在“首页”界面,下拉到一定距离的时候刷新微博数据
- 刷新数据的时候使用控件提示
- 新数据要加在旧数据的前面
- 刷新完毕隐藏刷新控件
- 刷新数据完毕,导航栏下方弹出一个提示框,提示刷新微博数量
- 直接使用系统自带的UIRefreshControl就可以做出动画效果
- 使用微博的获取微博API参数since_id可以控制加载的微博从哪个id开始
- 使用可变数组来拼接新旧微博数据
- 创建一个UILabel放在导航控制器view上来提示刷新数据(放在TableView上会被滚走)


// 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];
}

/** 弹出微博更新提示框 */
- (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];
}];
}];
}


//
// 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
// 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;
}

//
// 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
// 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] - 手动刷新微博的更多相关文章
- [iOS微博项目 - 2.0] - OAuth授权3步
A.概念 OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用 ...
- [iOS微博项目 - 4.0] - 自定义微博cell
github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内 ...
- [iOS微博项目 - 1.0] - 搭建基本框架
A.搭建基本环境 github: https://github.com/hellovoidworld/HVWWeibo 项目结构: 1.使用代码构建UI,不使用storyboard ...
- [iOS微博项目 - 4.5] - 每条微博的底部工具条
github: https://github.com/hellovoidworld/HVWWeibo A.每条微博的底部工具条 1.需求 每条微博底部都有一个工具条 显示3个按钮:评论.转发.赞 按钮 ...
- [iOS微博项目 - 4.2] - 设置转发微博背景
github: https://github.com/hellovoidworld/HVWWeibo A.转发微博部分的淡灰色背景 1.需求 转发微博部分需要设置背景色 使用图片作为背景 2.思路 ...
- iOS开发小技巧--微博项目中的键盘工具条
微博项目中的键盘工具条 项目中的键盘工具条不能使用inputAccessoryView,因为inputAccessoryView不能实现键盘隐藏的时候,工具条还显示在眼前,如图: 所以,果断决定将工具 ...
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
AJ分享,必须精品 一:效果 没有图文混排,也没有复杂的UI,仅仅是简单的显示出微博数据,主要介绍AFNetworking和SDWebImage的简单用法 二:加载数据AFNetworking AFN ...
随机推荐
- CodeForces Round #301 Div.2
今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. #in ...
- bzoj2396: 神奇的矩阵
与51nod1140一样.不过这题是多组数据的...坑.... #include<cstdio> #include<cstring> #include<cctype> ...
- I.MX6 ov5640 camera
/************************************************************************ * I.MX6 ov5640 camera * 说明 ...
- AIX 第7章 指令记录
要点: AIX文件系统的访问路径 AIX文件系统目录树 创建AIX文件系统 文件系统的卸载和删除 文件系统的自动挂载 文件系统的容量管理 文件系统的一致性管理 文件系统的卸载失败 文件系统的快照管理 ...
- 转深入学习heritrix---体系结构(Overview of the crawler)
Heritrix采用了模块化的设计,它由一些核心类(core classes)和可插件模块(pluggable modules)构成.核心类可以配置,但不能被覆盖,插件模块可以被由第三方模块取代. ( ...
- mysql 在大型应用中的架构演变
文正整理自:http://www.csdn.net/article/2014-06-10/2820160 可扩展性 架构的可扩展性往往和并发是息息相关,没有并发的增长,也就没有必要做高可扩展性的架构, ...
- 2016第20周四java基础概念
简单的说JDK=JRE+Java编译器.调试器.工具类库等:JRE=JVM(类似于jre目录下的bin)+必要运行的类库(类似于jre目录下的lib) JVM:Java Virtual Mechina ...
- 18个jQuery Mobile开发贴士和教程
jQuery Mobile 是 jQuery 在手机上和平板设备上的版本.jQuery Mobile 不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架.支持 ...
- ueditor-百度编辑器插件
1.官网地址:http://ueditor.baidu.com/website/index.html 2.定制化工具栏:(1)修改ueditor.config.js的toolsbar(2)在创建编辑器 ...
- Ioc注入方式写dubbo client(非set beans)
@Autowired注解的方式注解 Spring框架中进行注入式,使用@Autowired. @Autowired可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作,这里必须明确:@Auto ...