我在 UIScrollview 中添加 (400+) 个 UIButton,当我在 simulator 中滚动时,它运行良好,但在 iPad 中它缓慢滚动。
Here's我在做什么。
- (void)viewDidLoad { [scrollView setContentSize:CGSizeMake(180 * 24 , 22 * 40 + 200)]; for (int e=0; e<12; e++) { for(int i=0;i<24;i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.titleLabel.font = [UIFont systemFontOfSize:12.0f]; [btn setBackgroundColor:[UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1]]; CALayer *layer = btn.layer; layer.cornerRadius = 5.f; layer.masksToBounds = YES; layer.borderWidth = 1.f; layer.borderColor = [[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1] CGColor]; CGRect rect = btn.frame; rect.size.height = 40; rect.size.width = 50; rect.origin.x = 100 * i; rect.origin.y = 40 * e; btn.frame = rect; CAGradientLayer *shineLayer = [CAGradientLayer layer]; shineLayer.frame = layer.bounds; shineLayer.colors = [NSArray arrayWithObjects: (id)[UIColor colorWithWhite:0.9f alpha:0.5f].CGColor, (id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor, (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor, (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor, (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,nil]; [layer addSublayer:shineLayer]; UILabel *lblDes = [[UILabel alloc] initWithFrame:CGRectMake(0,0,50,40)]; lblDes.backgroundColor = [UIColor clearColor]; lblDes.numberOfLines = 3; lblDes.textColor = [UIColor colorWithRed:4.0/255.0 green:106.0/255.0 blue:200.0/255.0 alpha:1]; lblDes.textAlignment = UITextAlignmentCenter; lblDes.text = @"adf"; lblDes.tag = -1; lblDes.font = [UIFont systemFontOfSize:11.f]; [btn addSubview:lblDes]; [scrollView addSubview:btn]; } } [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
最佳答案
iOS 模拟器在 Mac 上运行,比实际设备快很多。
这完全取决于您的要求(为什么您需要这么多按钮),
我建议添加一个 UIView 作为 subview ,然后在 drawRect 中绘制必要的东西。然后您可以使用手势识别器找出用户点击的位置。 (根据 tdubik 的建议)
其他解决方案可以是 tableView。 (有可能重复使用单元格。您可以为每个单元格添加一个按钮)。
如果 tableView 不是您要查找的内容,并且 drawRect 太简单(因为它没有选择效果)- 您可以有一个隐藏的 UIButton,然后可以将其(处于突出显示状态)相应地放置在用户点击的位置,并在用户从屏幕上移除触摸时移除。
编辑
当使用 drawrect 时 - 然后您可以为使用 drawrect 绘制元素的 subview 添加手势识别器:
CustomViewClass *mView = [CustomViewClass alloc] init];
...
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];
singleTap.delegate = self;
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[mView addGestureRecognizer:singleTap];
然后:
- (void)handleTap:(UITapGestureRecognizer *)tap
{
CGPoint mPoint = [tap locationInView:tap.view];
int mOffset = 0; //beginning offset where buttons will start.
for(int i = 0; i < 20; i++) //how many rows of buttons? 20?
{
mOffset += 20; //your custom drawn button height + offset from previous button
if(mPoint.y < mOffset) //now check - if y tapped position is lower than current button.y + its height, then we know what button we tapped.
{
//do something - we found button row!!
break;
}
}
mOffset = 0; //beginning x offset
for(int i = 0; i < 20; i++) //how many columns buttons? 20?
{
mOffset += 20; //your custom drawn button width + offset from previous button
if(mPoint.x < mOffset) //now check - if x tapped position is lower than current button.x + its width, then we know what button we tapped.
{
//do something - we found button column!! at this point we know which button was tapped.
break;
}
}
}
如果您有二维按钮数组,请添加对 x 坐标的相同检查。
关于iOS - 在 UIScrollView 中添加多个 UIButton 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12910909/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
这里有一个很好的答案解释了如何在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”结果的