我已經通過按鈕實現了以下水平滾動,但似乎只在5秒後開始滾動。它卡住了。我猜,UI正在被阻塞。水平滾動的按鈕在iOS中
我有以下代碼來創建,在我ViewDidLoad()
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-100, self.view.frame.size.width, 100)];
int x = 10;
for (int i = 0; i < 10; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,10,80,80)];
[button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
[button setTag:i];
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:@selector(onClickofFilter:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
x += button.frame.size.width+10;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
有沒有什麼辦法讓它順利?
您的循環後添加滾動視圖,這就是爲什麼它的延遲,因爲for循環,然後完成它的負載,並設置按鈕=零,如果弧否則「[按鈕釋放]」添加按鈕 – 2014-10-17 02:38:51
更好的後使用集合視圖蘋果介紹UICollectionView試試吧 – 2014-10-17 02:39:51
scrollView.contentSize = CGSizeMake(x,scrollView.frame.size.height);這行代碼可能是錯誤的。它看起來應該是scrollView.contentSize = CGSizeMake(buttonWidth * x,buttonHeight + padding); – DBoyer 2014-10-17 02:49:28