2013-05-05 22 views
2

我正在處理包含主要類別和子類別的tableview。基本上主要類別是tableview節標題,當用戶點擊tableview節標題中的按鈕時,我設置我的tableview在tableview中顯示這些子類(行)。在UITableview部分視圖上的UI按鈕不動畫

一切都很好,但我想要做的是當用戶點擊部分標題中的「顯示子類別按鈕」時,我希望該按鈕能夠動畫和旋轉90度。該按鈕是一個UIButtonTypeDetailDisclosure類型,所以我想你得到了我想要的。

動畫工作時,我將動畫代碼添加到我的「viewForHeaderInSection」委託方法,但它不工作從外面。奇怪的是,點擊操作也起作用。繼承人的代碼(只是爲了把事情說清楚,用戶也可以直接點擊主類別以顯示該類別與手勢識別一切):

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

MainCategory* cat = [self.tableViewArray objectAtIndex:section]; 

UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(55, 0, 300, 44)]; 
UIView* imageView = [[UIView alloc]initWithFrame:CGRectMake(2, 2, 40, 40)]; 
imageView.backgroundColor = [UIColor darkGrayColor]; 
view.backgroundColor = [UIColor lightTextColor]; 
label.text = cat.name; 
label.backgroundColor = [UIColor clearColor]; 
[view addSubview:label]; 
[view addSubview:imageView]; 
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectMainCategory:)]; 
[view addGestureRecognizer:tapGesture]; 
view.tag = section; 
if([cat.subcategories count]>0){ 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    button.frame = CGRectMake(200, 0, 44, 44); 
    button.tag = section; 
    [button addTarget:self action:@selector(subCategoryClick:) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:button]; 
} 
return view; 
} 

而對於我的按鈕點擊操作:

- (IBAction)subCategoryClick:(UIButton*)sender{ 

    NSLog(@"click!"); 
    MainCategory* cat = [tableViewArray objectAtIndex:sender.tag]; 
    NSNumber* section = [NSNumber numberWithInt:sender.tag]; 
    if(![openSections containsObject:section]){ 
     [openSections addObject:section]; 


     NSMutableArray* indexPathsToAdd = [NSMutableArray array]; 
     for(int i = 0;i<[cat.subcategories count];i++){ 
      [indexPathsToAdd addObject:[NSIndexPath indexPathForRow:i inSection:sender.tag]]; 
     } 
     [self.tableView beginUpdates]; 
     [self.tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 
     [self rotateButton:sender isOpeningSection:YES]; 

    }else{ 
     [openSections removeObject:section]; 

     NSMutableArray* indexPathsToRemove = [NSMutableArray array]; 
     for(int i = 0;i<[cat.subcategories count];i++){ 
      [indexPathsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:sender.tag]]; 
     } 
     [self.tableView beginUpdates]; 
     [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 

     [self rotateButton:sender isOpeningSection:NO]; 
    } 
    [self.tableView reloadData]; 
} 

,最後,我的動畫方法(只是看它是否是動畫,不是最終方法):

- (void)rotateButton:(UIButton*)button isOpeningSection:(BOOL)isOpen{ 
    CABasicAnimation *halfTurn; 
    halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    halfTurn.fromValue = [NSNumber numberWithFloat:0]; 
    halfTurn.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)]; 
    halfTurn.duration = 0.5; 
    halfTurn.repeatCount = HUGE_VALF; 
    [[button layer] addAnimation:halfTurn forKey:@"180"]; 
} 

正如我所說的,當我在rotatebutton內容添加到viewForSection方法,該按鈕正在旋轉,但從舔事件調用時不會旋轉。我想我的目標是在這個方法錯誤的按鈕層..

感謝您的幫助!

回答

1

我猜想reloadData的調用迫使表視圖從tableView:viewForHeaderInSection:創建一個新視圖,該視圖立即從視圖層次結構中刪除舊視圖並將未加旋轉的視圖添加回來。

可能的解決方案:

  • 如果你已經在做insertRowsAtIndexPathsdeleteRowsAtIndexPaths動畫,你需要調用reloadData呢?除了旋轉動畫之外,最有可能的是破壞這些動畫。

  • 您可以提前創建您的部分標題(也許在viewDidLoad),只需返回參考文獻tableView:viewForHeaderInSection:即可。當您致電reloadData時,這將保持任何動畫完整無缺。

+0

沒有知道我不需要重新加載數據,當我使用insertrows或deleterows。我想知道爲什麼我的動畫表現出奇怪的笑聲謝謝 – dreampowder 2013-05-06 08:01:02

1

360 * PI/180 = 2PI。那是360度。使用M_PI/2。

+0

感謝您的答案,但旋轉度僅用於測試,主要問題是動畫不適用於「rotatebutton」方法被調用時的按鈕。 – dreampowder 2013-05-05 11:30:17

+1

嗯它是因爲你在點擊方法中調用reloadData?這將調用viewForHeader,在其中創建一個新按鈕!動畫發生在舊的 – Peteee24 2013-05-05 14:26:56