how to random placement of UIButton and value

2020-05-01 08:34发布

i have a question view and will show 4 answers, only 1 are correct.

but i do not want the same button to be always the correct answer.

i will like to know how do i do a random placement of 4 UIButton and value every time.

enter image description here

when user go to this question again the answer will be in differ button

enter image description here

my placement of X,y,W,H

button 1 5,219,230,45

button 2 5,273,230,45

button 3 244,224,230,45

button 4 244,273,230,45

8条回答
爷、活的狠高调
2楼-- · 2020-05-01 08:40

create your own class with these 4 buttons. In this class you can store the positions of your buttons. then you can add a member function (something like the following) which will draw them:

-(void)drawButtonsWithIncorrectAtIndex:(int)index
查看更多
手持菜刀,她持情操
3楼-- · 2020-05-01 08:47

I would suggest do not change the position of buttons rather what you can do is change the currentTitle property of the buttons randomly and once the user clicks the button ; you can get the currentTitle of the button and check if it is the correct answer or not.

查看更多
Explosion°爆炸
4楼-- · 2020-05-01 08:48

Out of 4 options only one is correct so why you worry, just give tag to your UIButton. In touchUpInside event or any action you've specified on it. Just match the correct answer tag with user touched button tag.

You should set some logic to set answer with different button tag randomly.

查看更多
\"骚年 ilove
5楼-- · 2020-05-01 08:57

You can create the buttons in same position always and randomize the labels on the button. If you are using an array to provide labels like val1, val2, val3 etc, you can randomize the array.

查看更多
手持菜刀,她持情操
6楼-- · 2020-05-01 09:03

I have implemented code for same at my side and it works fine.

-(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;
            }

        }

    }

}
查看更多
\"骚年 ilove
7楼-- · 2020-05-01 09:03

You should create button and add them as subView. When you need to put button at random location, you can use:

[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 )];
查看更多
登录 后发表回答