我正在处理一个包含主要类别和子类别的表格 View 。基本上主要类别是 tableview 部分标题,当用户单击 tableview 部分标题中的按钮时,我将 tableview 设置为在 tableview 中显示这些子类别(行)。
一切都很好,但我想做的是当用户单击部分标题中的“显示子类别按钮”时,我希望该按钮动画并旋转 90 度。该按钮是 UIButtonTypeDetailDisclosure 类型,所以我猜你得到了我想要的。
当我将动画代码添加到我的“viewForHeaderInSection”委托(delegate)方法时,动画有效,但它在外部不起作用。奇怪的是,点击操作也有效。这是代码(为了清楚起见,用户也可以只单击一个主类别以使用手势识别器显示该类别中的所有内容):
- (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];
}
最后,我的动画方法(只是看它是否动画,而不是 final方法):
- (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 事件调用时它不会旋转。我想我在这个方法中定位了错误的按钮层..
感谢您的帮助!
最佳答案
我猜对 reloadData 的调用会强制 TableView 从 tableView:viewForHeaderInSection: 创建一个新 View ,这会立即从 View 中删除旧 View 层次结构并添加未旋转的一个。
可能的解决方案:
如果您已经在 insertRowsAtIndexPaths 或 deleteRowsAtIndexPaths 中制作动画,您是否还需要调用 reloadData?除了旋转动画之外,它很可能会破坏这些动画。
您可以提前创建您的节标题(也许在 viewDidLoad 中)并只在 tableView:viewForHeaderInSection: 中返回引用。当您调用 reloadData 时,这将使所有动画保持完整。
关于ios - UITableview Section View 上的 UIbutton 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16383747/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle