[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 ...
随机推荐
- 【笨嘴拙舌WINDOWS】GDI(1)
GDI:Graphics Device Interface 图形设备接口. 操作系统从命令行界面到图形界面的过度是施乐公司实验室对计算机普及作出的不可估量的贡献,苹果公司乔布斯与微软公司比尔盖茨对其的 ...
- Spring MVC详细运行流程
- 涵盖网站基本使用的正则表达式的验证方法.cs
using System; using System.Text.RegularExpressions; using System.Text; using System.IO; using System ...
- [反汇编练习] 160个CrackMe之024
[反汇编练习] 160个CrackMe之024. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 安卓 Pickers(选择器)
概述 安卓提供了现成的对话框,让用户选择一个时间或日期.每一个选择器控制时间(小时,分钟,AM/PM)或日期(月,日,年)的每一部分的选择.使用这些选择器帮助 确保用户正确的,格式化的,和适合的选择一 ...
- arcgis9.3 执行python文件
1) 打开Python GUI 2) 选择菜单“File->Open”,打开你要执行的*.py文件 3) 选择菜单“Run->Run Module”,运行python文件 4)运行结果
- 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序
要求 其他必要产品 Database.com account 用户级别 全部 必需产品 PhoneGap Build 范例文件 Database.Com-PhoneGap-Sample 在这篇文章中, ...
- poj 2409(polya定理模板)
题意:给你n种颜色和m个小球,问你有多少种不同的方案! 分析:作为模板.. 代码实现: #include <iostream> #include <cstdio> #inclu ...
- api-ms-win-crt-runtime-l1-1-0.dll丢失问题
笔者是在安装python 3.5 后,启动时提示该文件丢失的问题的,如下所示.
- struts2中错误处理
定义一个 package,然后其他package都继承 这个package struts-global 就 有了这个错误处理功能了 然后再自己写个类 struts.xml <constant n ...