Balaji
Balaji

Reputation: 17

Change UIButton background images randomly

I want to change the background image of a UIButton randomly. I have some images (5) that I want to switch between randomly as background images for a UIButton. How can I implement this? Please help me out with this problem.

Upvotes: 1

Views: 278

Answers (4)

Martin
Martin

Reputation: 866

Create Static int variable set it's value to 0 initially. when button tap occurs increase value of that variable by 1 and pass that variable as argument to switch case. write code to change the image on each case and when count of static variable reaches 5 reset it to 0 again.

// write this at top of implementation file
static int count = 0;

// then on button click

 {

 UIImage *image;

 count++;
 switch(count)
 {
    case 1:
           image = [UIImage imageNamed:@"pic1.png"];
           break;
    case 2:
           image = [UIImage imageNamed:@"pic2.png"];
           break;
    case 3:
           image = [UIImage imageNamed:@"pic3.png"];
           break;
    case 4:
           image = [UIImage imageNamed:@"pic4.png"];
           break;   
    case 5:
           image = [UIImage imageNamed:@"pic5.png"];
           count = 0;
           break;
 }

  [button setBackgroundImage: image forState:UIControlStateNormal];
}

Upvotes: 0

Hmeedo
Hmeedo

Reputation: 94

//Create Array Of Images Name 
NSMutableArray *arrayOfImagesName=[[NSMutableArray alloc]initWithObjects:@"img1.png",@"img2.png",@"img3.png",nil];


//choose random image name from array
    int rndNumber=arc4random() % [arrayOfImagesName count];

//set image to the button 
    [myButton setBackgroundImage:[UIImage imageNamed:[arrayOfImagesName objectAtIndex:rndNumber]] forState:UIControlStateNormal];

Upvotes: 0

SAMIR RATHOD
SAMIR RATHOD

Reputation: 3506

try this

  //give Your Image name is P0.png, P1.png, P2.png,P3.png, P4.png
    int r = arc4random() % 5;
    NSString *strImg=[NSString stringWithFormat:@"P%@.png",r];
    UIImage *Image;
    Image = [UIImage imageNamed:strImg];
    [YourButton setBackgroundImage:Image forState:UIControlStateNormal];

Upvotes: 0

Groot
Groot

Reputation: 14261

How about something like this

NSInteger randomNumber =  arc4random_uniform(5); // random number, either 0,1,2,3 or 4
UIImage *randomImage = [UIImage imageNamed:[NSString stringWithFormat:@"img%u.png",randomNumber]];

//UIButton *myButton;
[myButton setBackgroundImage:randomImage forState:UIControlStateNormal];

Alternate solution that perhaps is easier for you to understand

NSInteger randomNumber =  arc4random_uniform(5); // random number, either 0,1,2,3 or 4
UIImage *randomImage;

switch (randomNumber) {
    case 0:
        randomImage = [UIImage imageNamed:@"img0.png"];
        break;
    case 1:
        randomImage = [UIImage imageNamed:@"img1.png"];
        break;
    case 2:
        randomImage = [UIImage imageNamed:@"img2.png"];
        break;
    case 3:
        randomImage = [UIImage imageNamed:@"img3.png"];
        break;
    case 4:
        randomImage = [UIImage imageNamed:@"img4.png"];
        break;
    default:
        break;
}
//UIButton *myButton;
[myButton setBackgroundImage:randomImage forState:UIControlStateNormal];

For future reference; you should NOT ask question where you more or less ask for someone to solve your problems for you. Include what you have tried even if you only have pseudo code.

Upvotes: 1

Related Questions