用户下拉微博view时,会出现一个刷新的图标,当有新数据时,我们希望为用户提示此次刷新了多少条数据,这个提示栏该怎么做呢?考虑一下。。。
OK,经过分析,可以考虑使用UIButton控件来做这个提示栏。
分析图:
实际效果图
在刷新控件的状态的改变方法中添加显示微博数据提示的方法:
/** * 监听刷新控件的状态改变(手动进入刷新状态才会调用这个方法) */ -(void)refreshControlStateChange:(UIRefreshControl*)refreshControl{ // 省略获取数据的代码... // 显示最新微博的数量(给用户一些友善的提示) statusFrameArray.count 更新的数据数 [self showNewStatusCount:statusFrameArray.count];}
显示更新提示数的具体方法:
/** * 显示微博数据 */-(void)showNewStatusCount:(int)count{ // 1.创建一个按钮 UIButton *btn = [[UIButton alloc] init]; // below:添加在导航栏的下面 [self.navigationController.view insertSubview:btn belowSubview:self.navigationController.navigationBar]; // 2.设置按钮背景图片和文字属性 btn.userInteractionEnabled = NO; [btn setBackgroundImage:[UIImage resizedImageWithName:@"timeline_new_status_background"] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; // 设置标题文字颜色 btn.titleLabel.font = [UIFont systemFontOfSize:14]; // 设置字体大小 if(count){ NSString *title = [NSString stringWithFormat:@"共有%d条新的微博", count]; [btn setTitle:title forState:UIControlStateNormal]; }else{ [btn setTitle:@"没有新的微博" forState:UIControlStateNormal]; } // 3.设置按钮的初始frame CGFloat btnH = 30; CGFloat btnY = 64 - btnH; CGFloat btnX = 2; CGFloat btnW = self.view.frame.size.width - 2 * btnX; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); // 4.通过动画移动按钮(按钮向下移动btnH + 1) [UIView animateWithDuration:1.0 animations:^{ btn.transform = CGAffineTransformMakeTranslation(0, btnH + 1); } completion:^(BOOL finished) { // 向下移动的动画执行完毕后 // 说明:该方法(animateKeyframesWithDuration)只能在IOS7以上版本才能执行,兼容低版本iOS6,需要用animateWithDuration方法 [UIView animateWithDuration:1.0 delay:0.7 options:(UIViewAnimationOptionCurveLinear) animations:^{ btn.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { // 将btn从内存中移除 [btn removeFromSuperview]; }]; /* [UIView animateKeyframesWithDuration:1.0 delay:1.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{ // 执行向上移动的动画(清空transform) btn.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { // 将btn从内存中移除 [btn removeFromSuperview]; }]; */ }];}
添加动画后的实际效果图: