2012-03-15 48 views
0

我有一個問題的觀點,並會顯示4個答案,只有1是正確的。如何隨機放置UIButton和值

但我不希望相同的按鈕始終是正確的答案。

我想知道我該如何隨機放置4個UIButton,並且每次都是值。

enter image description here

當用戶去這個問題再次答案將在不同的按鈕

enter image description here

我的X,Y,W,H

安置

按鈕1 5219230 ,45

button 2 5,273,230,45

按鈕3 244,224,230,45

按鈕4 244,273,230,45

+1

您可以爲每個按鈕設置標籤(1,2,3,4)。然後使用隨機函數查找標籤併爲該按鈕設置「正確」文本。 – 2012-03-15 05:53:08

回答

2

我碰巧正在研究類似的遊戲應用程序,所以得到了一些肯定會有所幫助的代碼。看看下面。請注意,我已經明白,您會爲正確和不正確的按鈕點擊調用'correctAnswerMethod'和'wrongAnswerMethod'。

// Define the four buttons in their respective frames, create the first button as the correct one, we'll shuffle it later. 
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button1 setTitle:@"Correct Answer" forState:UIControlStateNormal]; 
[button1 addTarget:self action:@selector(correctAnswerMethod:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button1]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setTitle:@"Wrong Answer 1" forState:UIControlStateNormal]; 
[button2 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button2]; 

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button3 setTitle:@"Wrong Answer 2" forState:UIControlStateNormal]; 
[button3 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button3]; 

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button4 setTitle:@"Wrong Answer 3" forState:UIControlStateNormal]; 
[button4 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button4]; 


//Create an array with the rectangles used for the frames 
NSMutableArray *indexArray = [NSMutableArray arrayWithObjects: 
           [NSValue valueWithCGRect:CGRectMake(5, 219, 230, 45)], 
           [NSValue valueWithCGRect:CGRectMake(5, 273, 230, 45)], 
           [NSValue valueWithCGRect:CGRectMake(244, 219, 230, 45)], 
           [NSValue valueWithCGRect:CGRectMake(244, 273, 230, 45)], nil]; 

//Randomize the array 
NSUInteger count = [indexArray count]; 
for (NSUInteger i = 0; i < count; ++i) { 
    int nElements = count - i; 
    int n = (arc4random() % nElements) + i; 
    [indexArray exchangeObjectAtIndex:i withObjectAtIndex:n]; 
} 

//Assign the frames 
button1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue]; 
button2.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue]; 
button3.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue]; 
button4.frame = [((NSValue *)[indexArray objectAtIndex:3]) CGRectValue]; 
+0

感謝Madhumal :) – Desmond 2012-03-16 01:54:16

1

您可以隨時創建同一位置的按鈕,並隨機按鈕上的標籤。如果您使用數組來提供像val1,val2,val3等標籤,則可以隨機化數組。

1

我建議不要改變按鈕的位置,而是你可以做的是隨機改變按鈕的currentTitle屬性,一旦用戶點擊按鈕;你可以得到按鈕的currentTitle並檢查它是否是正確的答案。

1

在4個選項中只有一個是正確的,所以爲什麼你擔心,只需給你的UIButton標籤。在touchUpInside事件中或您在其上指定的任何操作。只需將正確答案標籤與用戶觸摸的按鈕標籤相匹配即可

你應該設置一些邏輯來隨機設置不同按鈕標籤的答案。

+1

Wah Hemangsaheb ....很好的回答...來自:Krunal,'C'div。,Bvn ... – Krunal 2012-03-15 06:12:14

+0

@Goti,Krunal謝謝!很高興在這裏見到你! :) – Hemang 2012-03-15 09:53:54

1

您應該創建按鈕並將它們添加爲subView。當你需要把按鈕放在隨機位置時,你可以使用:

[button1 setFrame:CGRectMake(5,219,230,45)]; 
[button2 setFrame:CGRectMake(5,273,230,45)]; 
[button3 setFrame:CGRectMake(244,224,230,45)]; 
[button4 setFrame:CGRectMake(244,273,230,45)]; 
1

我不認爲你需要在這裏設置你的按鈕的x和y。只要設置你的按鈕的標籤,比如標籤1 - 4,生成隨機數,1 - 4(您的按鈕可能的標記),您的按鈕保存到NSArray的和不喜歡一個循環:

NSArray *buttonsArr = [[NSArray alloc]initWithObjects:button1,button2, button3, button4, nil]; 
int randomInt = (arc4random() % 4) + 1; 
for (int i = 0; i < [buttonsArr count]; i++) { 
    if ([(UIButton*)[buttonsArr objectAtIndex:i] tag] == randomInt) { 
     [[buttonsArr objectAtIndex:i] setTitle:@"correct" forState:UIControlStateNormal]; 
    }else{ 
     [[buttonsArr objectAtIndex:i] setTitle:@"wrong" forState:UIControlStateNormal]; 
    } 
} 
0

創建自己的類與這4個按鈕。在這個類中,你可以存儲你的按鈕的位置。那麼你可以添加一個成員函數(像下面這樣),這將吸引他們:

-(void)drawButtonsWithIncorrectAtIndex:(int)index 
3

我已經在我的身邊來實現代碼相同,它工作正常。

-(void)randomAllocation 
{ 
    NSMutableArray* allstring = [[NSMutableArray alloc]initWithObjects:@"Correct",@"Wrong",@"Wrong1",@"Wrong2", nil]; 

    NSMutableArray* outcomeOFrandom = [[NSMutableArray alloc]init]; 

    for (int i=0; i<[allstring count]; i++) { 
     int temp = 0; 

     while (temp==0) { 

      int randomInt = arc4random() % 4; 


      BOOL result = [outcomeOFrandom containsObject:[allstring objectAtIndex:randomInt]]; 
      if (result==FALSE) { 

       UIButton* btn = [[UIButton alloc]init]; 
       [btn setTitle:[allstring objectAtIndex:randomInt] forState:UIControlStateNormal]; 
       [btn setBackgroundColor:[UIColor blackColor]]; 

       NSLog(@"tital=%@",[allstring objectAtIndex:randomInt]); 

       if (i==0) { 
        [btn setFrame:CGRectMake(5,219,230,45)]; 
       } 
       else if(i==1) 
        [btn setFrame:CGRectMake(5,273,230,45)]; 
       } 
       else if(i==2) { 
        [btn setFrame:CGRectMake(244,224,230,45)]; 
       } 
       else if(i==3) { 
        [btn setFrame:CGRectMake(244,273,230,45)]; 
       } 

       [outcomeOFrandom addObject:[allstring objectAtIndex:randomInt]]; 
       [self.view addSubview:btn]; 
       [btn release]; 

       temp=1; 
       break; 
      } 

     } 

    } 

} 
+0

工作正常。謝謝 – Sujal 2012-03-15 08:48:05