我正在开发一个包含主要类别和子类别的表格视图。基本上主要类别是表格视图部分标题,当用户单击表格视图部分标题中的按钮时,我将我的表格视图设置为在表格视图中显示这些子类别(行)。
一切都很好,但我想要做的是当用户点击部分标题中的“显示子类别按钮”时,我希望该按钮动画并旋转 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 方法时,按钮正在旋转,但从 lick 事件调用时它不会旋转。我想我在这种方法中瞄准了错误的按钮层..
感谢您的帮助!