码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发项目篇—31提示最新微博数

时间:2014-07-16 18:46:11      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   strong   

iOS开发项目篇—31提示最新微博数

一、简单说明

1.导入图片素材

bubuko.com,布布扣

2.关于提示条的位置分析

原本的显示情况:

bubuko.com,布布扣

说明:滚动tableView对它没有任何的影响,可以知道提示条的父控件不应该是tableView

加入提示条之后的情况:

bubuko.com,布布扣   bubuko.com,布布扣

解决方案:

bubuko.com,布布扣

说明

(1)导航条是导航控制器的view子控件,可以把提示条添加到导航控制器的view上,当刷新的时候,有view调整提示条的位置。

(2)关于改变y的值以及transform的选择,如果是动画执行完成之后需要回复到以前的位置,那么建议使用transform

(3)让刷新控件自动进入到刷新状态,注意:通过代码并不会触发代理代理监听方法,只有手动拖动才会触发。

 

二、代码实现

YYHomeTableViewController.m文件

  1 //
  2 //  YYHomeTableViewController.m
  3 //
  4 
  5 #import "YYHomeTableViewController.h"
  6 #import "YYOneViewController.h"
  7 #import "YYTitleButton.h"
  8 #import "YYPopMenu.h"
  9 #import "YYAccountModel.h"
 10 #import "YYAccountTool.h"
 11 #import "AFNetworking.h"
 12 #import "UIImageView+WebCache.h"
 13 #import "YYUserModel.h"
 14 #import "YYStatusModel.h"
 15 #import "MJExtension.h"
 16 
 17 @interface YYHomeTableViewController ()<YYPopMenuDelegate>
 18 @property(nonatomic,assign)BOOL down;
 19 @property(nonatomic,strong)NSMutableArray *statuses;
 20 @end
 21 
 22 @implementation YYHomeTableViewController
 23 
 24 #pragma mark- 懒加载
 25 -(NSMutableArray *)statuses
 26 {
 27     if (_statuses==nil) {
 28         _statuses=[NSMutableArray array];
 29     }
 30     return _statuses;
 31 }
 32 - (void)viewDidLoad
 33 {
 34     [super viewDidLoad];
 35     
 36     //设置导航栏内容
 37     [self setupNavBar];
 38     
 39     //集成刷新控件
 40     [self setupRefresh];
 41 }
 42 
 43 //集成刷新控件
 44 -(void)setupRefresh
 45 {
 46     // 1.添加下拉刷新控件
 47     UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
 48     [self.tableView addSubview:refreshControl];
 49     
 50     //2.监听状态
 51     [refreshControl addTarget:self action:(@selector(refreshControlStateChange:)) forControlEvents:UIControlEventValueChanged];
 52     
 53     //3.让刷新控件自动进入到刷新状态
 54     [refreshControl beginRefreshing];
 55     
 56     //4.手动调用方法,加载数据
 57     //模拟网络延迟,延迟2.0秒
 58     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 59         [self refreshControlStateChange:refreshControl];
 60     });
 61 }
 62 
 63 /**
 64  *  当下拉刷新控件进入刷新状态(转圈圈)的时候会自动调用
 65  */
 66 -(void)refreshControlStateChange:(UIRefreshControl *)refreshControl
 67 {
 68     //1.获得请求管理者
 69     AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
 70     
 71     //2.封装请求参数
 72     NSMutableDictionary *params=[NSMutableDictionary dictionary];
 73     params[@"access_token"] =[YYAccountTool accountModel].access_token;
 74     //取出当前微博模型中的第一条数据,获取第一条数据的id
 75     YYStatusModel *firstStatus=[self.statuses firstObject];
 76     if (firstStatus) {
 77         params[@"since_id"]=firstStatus.idstr;
 78     }
 79     
 80     //3.发送Get请求
 81     [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary*accountDict) {
 82         // 微博字典 -- 数组
 83         NSArray *statusDictArray = accountDict[@"statuses"];
 84         //微博字典数组---》微博模型数组
 85        NSArray *newStatuses =[YYStatusModel objectArrayWithKeyValuesArray:statusDictArray];
 86         
 87         //把新数据添加到旧数据的前面
 88         NSRange range=NSMakeRange(0, newStatuses.count);
 89         NSIndexSet *indexSet=[NSIndexSet indexSetWithIndexesInRange:range];
 90         [self.statuses insertObjects:newStatuses atIndexes:indexSet];
 91         YYLog(@"刷新了--%d条新数据",newStatuses.count);
 92         
 93         //重新刷新表格
 94         [self.tableView reloadData];
 95         //让刷新控件停止刷新(回复默认的状态)
 96         [refreshControl endRefreshing];
 97       
 98         [self showNewStatusesCount:newStatuses.count];
 99         
100     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
101         YYLog(@"请求失败");
102         //让刷新控件停止刷新(回复默认的状态)
103         [refreshControl endRefreshing];
104     }];
105 
106 }
107 
108 /**
109  *  提示用户最新的微博数量
110  *
111  *  @param count 最新的微博数量
112  */
113 -(void)showNewStatusesCount:(int)count
114 {
115     //1.创建一个label
116     UILabel *label=[[UILabel alloc]init];
117     
118     //2.设置label的文字
119     if (count) {
120         label.text=[NSString stringWithFormat:@"共有%d条新的微博数据",count];
121     }else
122     {
123         label.text=@"没有最新的微博数据";
124     }
125     
126     //3.设置label的背景和对其等属性
127     label.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithName:@"timeline_new_status_background"]];
128     label.textAlignment=UITextAlignmentCenter;
129     label.textColor=[UIColor whiteColor];
130     
131     //4.设置label的frame
132     label.x=0;
133     label.width=self.view.width;
134     label.height=35;
135 //    label.y=64-label.height;
136    label.y=self.navigationController.navigationBar.height+20-label.height;
137     
138     //5.把lable添加到导航控制器的View上
139 //    [self.navigationController.view addSubview:label];
140     //把label添加到导航控制器上,显示在导航栏的下面
141     [self.navigationController.view insertSubview:label belowSubview:self.navigationController.navigationBar];
142     
143     //6.设置动画效果
144     CGFloat duration=0.75;
145     //设置提示条的透明度
146     label.alpha=0.0;
147    [UIView animateWithDuration:duration animations:^{
148        //往下移动一个label的高度
149        label.transform=CGAffineTransformMakeTranslation(0, label.height);
150        label.alpha=1.0;
151    } completion:^(BOOL finished) {//向下移动完毕
152        
153        //延迟delay秒的时间后,在执行动画
154        CGFloat delay=0.5;
155        
156        [UIView animateKeyframesWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseOut animations:^{
157            
158            //恢复到原来的位置
159            label.transform=CGAffineTransformIdentity;
160            label.alpha=0.0;
161            
162        } completion:^(BOOL finished) {
163            
164            //删除控件
165            [label removeFromSuperview];
166        }];
167    }];
168 }
169 
170 /**
171  UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // 开始:由慢到快,结束:由快到慢
172  UIViewAnimationOptionCurveEaseIn               = 1 << 16, // 由慢到块
173  UIViewAnimationOptionCurveEaseOut              = 2 << 16, // 由快到慢
174  UIViewAnimationOptionCurveLinear               = 3 << 16, // 线性,匀速
175  */
176 
177 /**设置导航栏内容*/
178 -(void)setupNavBar
179 {
180     self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch" highImageName:@"navigationbar_friendsearch_highlighted" target:self action:@selector(friendsearch)];
181     self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_pop" highImageName:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];
182     
183     //设置导航栏按钮
184     YYTitleButton *titleButton=[[YYTitleButton alloc]init];
185     //设置文字
186     [titleButton setTitle:@"首页" forState:UIControlStateNormal];
187     //设置图标
188     [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
189     //设置背景
190     [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
191     
192     //设置尺寸
193     titleButton.width=100;
194     titleButton.height=35;
195     //监听按钮的点击事件
196     [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
197     self.navigationItem.titleView=titleButton;
198 }
199 -(void)titleButtonClick:(UIButton *)titleButton
200 {
201 
202         [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
203         
204         UITableView *tableView=[[UITableView alloc]init];
205         [tableView setBackgroundColor:[UIColor yellowColor]];
206         YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView];
207         [menu showInRect:CGRectMake(60, 55, 200, 200)];
208         menu.dimBackground=YES;
209 
210     menu.arrowPosition=YYPopMenuArrowPositionRight;
211         menu.delegate=self;
212 }
213 
214 
215 #pragma mark-YYPopMenuDelegate
216 //弹出菜单
217 -(void)popMenuDidDismissed:(YYPopMenu *)popMenu
218 {
219     YYTitleButton *titleButton=(YYTitleButton *)self.navigationItem.titleView;
220     [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
221 }
222 -(void)pop
223 {
224     YYLog(@"---POP---");
225 }
226 -(void)friendsearch
227 {
228     //跳转到one这个子控制器界面
229     YYOneViewController *one=[[YYOneViewController alloc]init];
230     one.title=@"One";
231     //拿到当前控制器
232     [self.navigationController pushViewController:one animated:YES];
233     
234 }
235 
236 #pragma mark - Table view data source
237 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
238 {
239     return self.statuses.count;
240 }
241 
242 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
243 {
244     static NSString *ID = @"cell";
245     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
246     if (!cell) {
247         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
248     }
249     
250       //取出这行对应的微博字典数据,转换为数据模型
251     YYStatusModel *status=self.statuses[indexPath.row];
252     cell.textLabel.text=status.text;
253     cell.detailTextLabel.text=status.user.name;
254     NSString *imageUrlStr=status.user.profile_image_url;
255     [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
256     
257     return cell;
258 }
259 
260 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
261 {
262     //点击cell的时候,跳到下一个界面
263     UIViewController *newVc = [[UIViewController alloc] init];
264     newVc.view.backgroundColor = [UIColor redColor];
265     newVc.title = @"新控制器";
266     [self.navigationController pushViewController:newVc animated:YES];
267 }
268 
269 @end

实现效果:

bubuko.com,布布扣    bubuko.com,布布扣

 

 

iOS开发项目篇—31提示最新微博数,布布扣,bubuko.com

iOS开发项目篇—31提示最新微博数

标签:style   blog   http   color   使用   strong   

原文地址:http://www.cnblogs.com/wendingding/p/3845017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!