1. 集成 自带的 下拉属性控件 ---------HWHomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 集成刷新控件
[self setupRefresh];
}
/** 集成 下拉刷新 控件 */
- (void)setupRefresh
{
// 1.添加刷新控件
UIRefreshControl *control = [[UIRefreshControl alloc] init];
// 只有用户通过手动下拉刷新,才会触发UIControlEventValueChanged事件
[control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:control];
// 2.马上进入刷新状态(仅仅是显示刷新状态,并不会触发UIControlEventValueChanged事件)
[control beginRefreshing];
// 3.马上加载数据
[self refreshStateChange:control];
}
----------------------------------------------------------------------------------------------------------
/**
* UIRefreshControl进入刷新状态:加载最新的数据
*/
- (void)refreshStateChange:(UIRefreshControl *)control
{
// 1.请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.拼接请求参数
HWAccount *account = [HWAccountTool account];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = account.access_token;
// 取出最前面的微博(最新的微博,ID最大的微博)
HWStatus *firstStatus = [self.statuses firstObject];
if (firstStatus) {
// 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0
params[@"since_id"] = firstStatus.idstr;
}
// 3.发送请求
[mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// 将 "微博字典"数组 转为 "微博模型"数组
NSArray *newStatuses = [HWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
// 将最新的微博数据,添加到总数组的最前面
NSRange range = NSMakeRange(0, newStatuses.count);
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
[self.statuses insertObjects:newStatuses atIndexes:set];
// 刷新表格
[self.tableView reloadData];
// 结束刷新刷新
[control endRefreshing];
// 显示最新微博的数量
[self showNewStatusCount:newStatuses.count];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HWLog(@"请求失败-%@", error);
// 结束刷新刷新
[control endRefreshing];
}];
}
/**
* 显示最新微博的数量
*
* @param count 最新微博的数量
*/
- (void)showNewStatusCount:(int)count
{
// 1.创建label
UILabel *label = [[UILabel alloc] init];
// colorWithPatternImage: 这个方法用在平铺的场合。
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"timeline_new_status_background"]];
label.width = [UIScreen mainScreen].bounds.size.width;
label.height = 35;
// 2.设置其他属性
if (count == 0) {
label.text = @"没有新的微博数据,稍后再试";
} else {
label.text = [NSString stringWithFormat:@"共有%d条新的微博数据", count];
}
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:16];
// 3.添加
label.y = 64 - label.height;
// 将label添加到导航控制器的view中,并且是盖在导航栏下边
[self.navigationController.view insertSubview:label belowSubview:self.navigationController.navigationBar];
// 4.动画
// 先利用1s的时间,让label往下移动一段距离
CGFloat duration = 1.0; // 动画的时间
[UIView animateWithDuration:duration animations:^{
// label.y += label.height;
label.transform = CGAffineTransformMakeTranslation(0, label.height);
} completion:^(BOOL finished) {
// 延迟1s后,再利用1s的时间,让label往上移动一段距离(回到一开始的状态)
CGFloat delay = 1.0; // 延迟1s
// UIViewAnimationOptionCurveLinear:匀速
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
// label.y -= label.height;
label.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[label removeFromSuperview];
}];
}];
// 如果某个动画执行完毕后,又要回到动画执行前的状态,建议使用transform来做动画
}