1014-26-首頁07-最新微博數量-------顯示最新微博數量動畫---平鋪圖片---數組插入數組,1014-26-07-

來源:互聯網
上載者:User

1014-26-首頁07-最新微博數量-------顯示最新微博數量動畫---平鋪圖片---數組插入數組,1014-26-07-

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來做動畫
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.