我正在處理包含主要類別和子類別的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方法,該按鈕正在旋轉,但從舔事件調用時不會旋轉。我想我的目標是在這個方法錯誤的按鈕層..
感謝您的幫助!
沒有知道我不需要重新加載數據,當我使用insertrows或deleterows。我想知道爲什麼我的動畫表現出奇怪的笑聲謝謝 – dreampowder 2013-05-06 08:01:02