为阵列中的按钮随机设置图像,从阵列中取出按钮的cgrect

时间:2015-06-20 04:47:26

标签: ios objective-c nsarray

我的故事板中有42个按钮,我拍摄了一系列图像。在这个数组中共有7个图像。

NSArray *arrimg=[NSArray arrayWithObjects:@"book.png", @"lock.png", @"Exclamation.png", @"game.png", @"Music.png", @"Videos.png", @"Camera32.png", nil];

那么每次按钮时我怎么能随机拍摄图像呢?

我应该收集按钮还是做其他事情?

注意: - 我没有以编程方式使用按钮。

一个新的问题立场:如何从阵列中取出所有42个按钮的CGRect并在屏幕上随机显示。

NSMutableArray *mutarr=[[NSMutableArray alloc]init];
NSMutableArray *array=[NSMutableArray arrayWithArray:mutarr];
[array addObject:[NSValue valueWithCGRect:CGRectMake(50, 50, 47, 25)]];
[array addObject:[NSValue valueWithCGRect:CGRectMake(97, 50, 47, 25)]];
[array addObject:[NSValue valueWithCGRect:CGRectMake(144, 50, 47, 25)]];
CGRect someRect1 = [[array objectAtIndex:0] CGRectValue];
CGRect someRect2 = [[array objectAtIndex:0] CGRectValue];
CGRect someRect3 = [[array objectAtIndex:0] CGRectValue];

我不知道该怎么做。

3 个答案:

答案 0 :(得分:1)

为0到41的每个42按钮添加标签

- (void)viewDidLoad {
        [super viewDidLoad];
        arrimg=[NSArray arrayWithObjects:@"book.png", @"lock.png", @"Exclamation.png", @"game.png", @"Music.png", @"Videos.png", @"Camera32.png", nil];
        NSTimer *timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(performAction) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    }
    -(void)performAction {

    NSInteger randomNumberForImage = rand() % (arrimg.count);
    NSInteger randomNumberForButton=rand()%(numberOfButton);// numberOfButton=42;
    UIButton *btn=[self viewWithTag:randomNumberForButton];
    [btn setImage:[UIImage imageNamed:[arrimg objectAtIndex:randomNumberForImage]] forState:UIControlStateNormal];

}

答案 1 :(得分:0)

尝试这个......

int length = [myArray count];
// Get random value between 0 and 99 if your count is 100
int randomindex = arc4random() % length;

Object randomObj = [myArray objectAtIndex:randomindex];

在函数中添加上述代码,并从viewWillAppear中调用它。

答案 2 :(得分:-1)

我试过这个并且有效......

在viewdidload中:

arrimg=[[NSMutableArray alloc]initWithObjects:@"book.png",@"lock.png",@"Exclamation.png",@"game.png",@"Music.png",@"Videos.png",@"Camera32.png", nil];


NSMutableArray *mutArrayb = [NSMutableArray arrayWithArray:arrimg];
NSUInteger count = [mutArrayb count];
if (count > 1)
{
    for (NSUInteger i = count - 1; i > 0; --i) {
        [mutArrayb exchangeObjectAtIndex:i withObjectAtIndex:arc4random_uniform((int32_t)(i + 1))];
        NSLog(@"%@",[mutArrayb objectAtIndex:i]);
    }
}
//rndmim=[[NSMutableArray alloc]initWithCapacity:7];
rndmim = [NSMutableArray arrayWithArray:mutArrayb];
for (int i=0; i<7; i++)
{
    NSLog(@"%d  %@",i,[rndmim objectAtIndex:i]);
    [btn1 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn3 setImage:[UIImage imageNamed:[rndmim objectAtIndex:2]] forState:UIControlStateNormal];
    [btn4 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn5 setImage:[UIImage imageNamed:[rndmim objectAtIndex:4]] forState:UIControlStateNormal];
    [btn6 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn7 setImage:[UIImage imageNamed:[rndmim objectAtIndex:6]] forState:UIControlStateNormal];
    [btn8 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn9 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn10 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn11 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn12 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn13 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn14 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn15 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn16 setImage:[UIImage imageNamed:[rndmim objectAtIndex:2]] forState:UIControlStateNormal];
    [btn17 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn18 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn19 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn20 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn21 setImage:[UIImage imageNamed:[rndmim objectAtIndex:2]] forState:UIControlStateNormal];
    [btn22 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn23 setImage:[UIImage imageNamed:[rndmim objectAtIndex:4]] forState:UIControlStateNormal];
    [btn24 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn25 setImage:[UIImage imageNamed:[rndmim objectAtIndex:6]] forState:UIControlStateNormal];
    [btn26 setImage:[UIImage imageNamed:[rndmim objectAtIndex:4]] forState:UIControlStateNormal];
    [btn27 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn28 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn29 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn30 setImage:[UIImage imageNamed:[rndmim objectAtIndex:6]] forState:UIControlStateNormal];
    [btn31 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn32 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn33 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn34 setImage:[UIImage imageNamed:[rndmim objectAtIndex:6]] forState:UIControlStateNormal];
    [btn35 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn36 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn37 setImage:[UIImage imageNamed:[rndmim objectAtIndex:0]] forState:UIControlStateNormal];
    [btn38 setImage:[UIImage imageNamed:[rndmim objectAtIndex:1]] forState:UIControlStateNormal];
    [btn39 setImage:[UIImage imageNamed:[rndmim objectAtIndex:2]] forState:UIControlStateNormal];
    [btn40 setImage:[UIImage imageNamed:[rndmim objectAtIndex:3]] forState:UIControlStateNormal];
    [btn41 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
    [btn42 setImage:[UIImage imageNamed:[rndmim objectAtIndex:5]] forState:UIControlStateNormal];
}

当我回到之前的视图并在此视图中再次返回时,图像再次发生变化......它运作良好....