I’m adding (400+) UIButtons in UIScrollview, When I scrolls in simulator its working well, but in iPad its scrolling slowly.
Here’s what I’m doing.
- (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 simulator runs on Mac, which is a lot faster than actual Device.
It all depends on Your requirements (why You need so many buttons),
I would suggest adding an UIView as subview, and then in drawRect – draw necessary things. Then You could use gesture recogniser to find out location where user tapped. (as suggested from tdubik)
Other solution could be tableView. (which has possibility to reuse cells. You could add a button to each cell).
If tableView is not what you are looking for, and drawRect is too plain (as it will be without selection effects) – You could have one hidden UIButton, which (in highlighted state) then could be placed correspondingly where user tapped, and removed, when user removes touches from screen.
EDIT
When using drawrect – You can then add gesture recognizer for subview in which elements are drawn using drawrect:
…
and then :
If You have 2 dimension button array, add the same checking for x coordinates.