2012-04-23 66 views
11

我很努力通過動畫去除tableview的頁眉/頁腳內容。我相信我可以在標題內容本身上調用removeFromSuperview,或者只將標題分配給nil,但我不知道如何爲此設置動畫。一個簡單的動畫塊不會做任何事情 - 如何淡入淡出視圖(在這種情況下是標籤)進/出?動畫UITableView的頁眉和頁腳

+0

你使用UITableViewController,UITableViewDelegate或UITableViewDataSource實例或其他視圖控制器類嗎?如果您使用的是UITableViewDataSource,則應該使用tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)部分以實際刪除標題和tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)部分以編程方式刪除頁腳。 – 2012-04-23 23:05:50

+0

我正在使用tableview控制器。只是爲了澄清,我想淡出表格的標題視圖,而不是部分標題 – 2012-04-23 23:11:19

回答

24

我寫了一個例子,說明如何使用動畫塊完全刪除標題視圖。

[UIView beginAnimations:nil context:NULL]; 
[self.tableView setTableHeaderView:nil]; 
[UIView commitAnimations]; 

這當然假設這個動畫是在你的UITableViewController類中被調用的。

簡單地從超級視圖中刪除標題視圖不起作用,因爲表視圖不知道要調整自己的大小來填充過去是標題視圖。標題視圖的設置使表格視圖動畫刷新自身。

如果這不起作用,請檢查您的tableView實例的賦值。

1

我發現下面的兩個選項都工作正常:

選項1

[UIView animateWithDuration:0.15 
         delay:0.0 
     options: UIViewAnimationCurveEaseOut 
       animations:^{deleteLabel.alpha = 0;} 
      completion:nil]; 

選項2

[UIView beginAnimations:nil context:nil]; 
deleteLabel.alpha = 0.0; 
[UIView commitAnimations]; 
+2

對於任何在此之後的人,只需稍作說明,上面的選項應該是UIViewAnimationOptionCurveEaseOut – 2015-02-13 00:44:31

0

這是工作完美 「現況」,我不關心動畫

[myPrettyViewControllerInstance letUpdatePrettyViewNow]; 
    [[self tableView] beginUpdates]; 
    [[self tableView] setTableHeaderView:[myPrettyViewControllerInstance view]]; 
    [[self tableView] endUpdates]; 

但如果我們沒必要更

[[self tableView] setTableHeaderView:nil]; 

這就是所有我們需要,真的(如果它沒有淡出 - 檢查的

[[self tableView] setTableHeaderView:nil]; 

的條件,我們沒有必要

[UIView beginAnimations:nil context:NULL]; 
    [[self tableView] setTableHeaderView:nil]; 
    [UIView commitAnimations]; 

nop

[[self tableView] beginUpdates]; 
    [[self tableView] setTableHeaderView:nil]; 
    [[self tableView] endUpdates]; 

NOP

[[self tableView] setTableHeaderView:nil]; 
    [[self tableView] reloadData]; 

野應..

+0

這適用於單元格,但不適用於表格標題。 – Gujamin 2013-02-27 23:08:43

+0

對不起,但它適用於標題(不知道單元格)。我在我最近的項目中使用這個,所有的工作。你能提供源代碼嗎? – WINSergey 2013-02-28 09:03:55

+0

它不適用於插入表格標題視圖。使用故事板和自動佈局。視圖剛剛出現。它不動畫英寸 – Gujamin 2013-03-05 19:55:41

8

我結束了以下簡單的解決方案。它動畫化了標題的刪除。

[self.tableView beginUpdates]; 
self.tableView.tableHeaderView = nil; 
[self.tableView endUpdates]; 

我意識到這是在WINSergey的迴應中提到的,但我發現他的解釋有點混亂。

1

我有同樣的問題,我想控制的標題是如何去除,這裏是我的方法:

在viewDidLoad中:

UIView *transView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320.f, 200.f)]; 
transView.backgroundColor = [UIColor clearColor]; 
self.headerView = transView; 
[transView release]; 

[self.tableView setTableHeaderView:self.headerView]; 

然後,當你需要刪除標題:

[UIView animateWithDuration:.3f 
         delay:.0f 
        options: UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        self.headerView.frame = CGRectMake(0.0f, 0.0f, 320.f, 0.0f); 
       } 
       completion:^(BOOL finished) { 
         [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
       }]; 
0

我不得不放2個動畫來讓它工作。

[UIView animateWithDuration:2.0f animations:^{ 
     mylbl.alpha = 0.1 ; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:2.0f animations:^{ 
      mylbl.alpha = 1.0 ; 
     }]; 
    }];