2012-05-09 15 views

回答

0

使用arc4random()

,改變你的按鈕

的位置在你.h fileUIButton *buttonsarray[10];

在你.m file

// Make array of button using following way.I used this method to creates button and you can use yours. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

     float y = 5; 
     int x = 0; 
     int count = 0; 
     for (int i = 0 ; i < 10; i++) 
    { 
     count ++; 
     buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     buttonsarray[i].frame = CGRectMake(x, y, 100, 100); 
     [buttonsarray[i] setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal]; 
     x = x + 105; 
     [self.view addSubview:b[i]]; 
     if(count == 3) 
     { 
     count = 0; 
     x = 0; 
     y = y+ 105; 
     } 


    } 
} 


    // This function will soufflé your buttons 
- (IBAction)btnClicked:(id)sender 
    { 
    int n = 10; 
    int swaper; 
    for (int i = 0 ; i < 10 ; i++) 
    { 
     int r = arc4random()%n; 
     if(r != swaper){ 
     swaper = r; 
     CGRect r1 = buttonsarray[i].frame;  
     buttonsarray[i].frame = buttonsarray[swaper].frame; 
     buttonsarray[swaper].frame = r1; 

     n--; 
    } 

    } 
} 

希望,這會幫助你..

0

你可以做到這一點

  1. 做一組,你需要點存儲爲字符串CGPoints數組。
  2. 在layoutSubViews方法設置是這樣的:

    -(void)layoutSubViews{ 
        [self.subviews enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) { 
         CGPoint newPoint = CGPointFromString([positionsArray objectAtIndex:idx]); 
        object.frame = CGRectMake(newPoint.x,newPoint.y,object.frame.size.width,object.frame.size.height); 
    }]; 
    

    }

  3. 然後,你將需要重新洗牌的位置陣列中的位置,你可以看到一個示例方法在這裏:How to Shuffle an Array

  4. 現在,您每次需要清空頭寸時,都可以在視圖上致電-(void)setNeedsLayout

有更多的選擇,但這是我第一次想到的。

好運

相關問題