2014-10-17 110 views
2

我已經通過按鈕實現了以下水平滾動,但似乎只在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]; 

Image showing a row of green buttons on a red background.

有沒有什麼辦法讓它順利?

+0

您的循環後添加滾動視圖,這就是爲什麼它的延遲,因爲for循環,然後完成它的負載,並設置按鈕=零,如果弧否則「[按鈕釋放]」添加按鈕 – 2014-10-17 02:38:51

+2

更好的後使用集合視圖蘋果介紹UICollectionView試試吧 – 2014-10-17 02:39:51

+0

scrollView.contentSize = CGSizeMake(x,scrollView.frame.size.height);這行代碼可能是錯誤的。它看起來應該是scrollView.contentSize = CGSizeMake(buttonWidth * x,buttonHeight + padding); – DBoyer 2014-10-17 02:49:28

回答

7

請檢查該代碼:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)]; 

    int x = 0; 
    CGRect frame; 
    for (int i = 0; i < 10; i++) { 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    if (i == 0) { 
     frame = CGRectMake(10, 10, 80, 80); 
    } else { 
     frame = CGRectMake((i * 80) + (i*20) + 10, 10, 80, 80); 
    } 

    button.frame = frame; 
    [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]; 

    if (i == 9) { 
     x = CGRectGetMaxX(button.frame); 
    } 

    } 

    scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height); 
    scrollView.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:scrollView]; 
+0

當然我會檢查並回復你 – 2014-10-17 03:02:22

+0

你試過了嗎?你可以替換x = CGRectGetMaxX(button.frame);其中x = CGRectGetMaxX(button.frame)+ 10;在scrollView – 2014-10-17 03:30:56

+0

的末尾添加額外的空間,它的工作很好,謝謝julie – 2014-10-17 19:04:46

0

下面是SWIFT 2.2版本以供參考類似的代碼。

let scrollView = UIScrollView(frame: CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.width,35)) 
    var x = 0 
    var frame : CGRect? 



    for i in 0..<7 { 

    let button = UIButton(type: .Custom) 

     if i == 0 { 

      frame = CGRectMake(10, 5, 80, 20) 

     }else{ 

      frame = CGRectMake(CGFloat(i * 120), 5, 80, 20) 


     } 


     button.frame = frame! 
     button.tag = i 
     button.backgroundColor = UIColor.clearColor() 
     button.addTarget(self, action: #selector(ProgramacioViewController.changeContentOnButtonSelection(_:)), forControlEvents: .TouchUpInside) 
     scrollView.addSubview(button) 


    } 


    scrollView.contentSize = CGSizeMake(840, scrollView.frame.size.height) 
    scrollView.backgroundColor = UIColor.clearColor() 
    self.view.addSubview(scrollView) 
0
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)]; 

    int x = 0; 

    NSUInteger value= [userInfo count]; 
    for (int i = 0; i < value; i++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.titleLabel.textAlignment = UITextAlignmentCenter; 
     [button setFont:[UIFont fontWithName:@"Lato-Medium" size:12]]; 
     button.layer.borderWidth = 1.0f; 
     button.layer.cornerRadius = 10; 
     button.layer.borderColor = [UIColor whiteColor].CGColor; 
     button.clipsToBounds = YES; 
     [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal]; 
     [button setTag:i]; 
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [button setBackgroundColor:[UIColor clearColor]]; 
     button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     button.titleLabel.textAlignment = NSTextAlignmentCenter; 
     [button addTarget:self action:@selector(onClickSwipeButton:) forControlEvents:UIControlEventTouchUpInside]; 
     button.frame = CGRectMake((i * 100) + (i*10) + 10, 0, 100, 35); 
     button.tag = i; 
     x = CGRectGetMaxX(button.frame); 
     [scrollView addSubview:button]; 

    } 
    scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height); 
    scrollView.backgroundColor = [UIColor clearColor]; 
    [self.randomView addSubview:scrollView];