美文网首页
随机分配4个图片到四个按钮中

随机分配4个图片到四个按钮中

作者: 指尖的世界 | 来源:发表于2017-08-02 14:10 被阅读4次

-(void)RandomButtonImage {

NSMutableArray *images = [NSMutableArray arrayWithObjects:@"Character_1_1.png",@"Character_1_2.png",@"Character_2_1.png",@"Character_2_2.png" nil];

NSArray *Buttons = [NSArray arrayWithObjects:AnsButton1,AnsButton2,AnsButton3,AnsButton4, nil];

for (UIButton *btn in Buttons) {

int randomIndex = random() % images.count;

UIImage *img = [UIImage imageNamed:[images objectAtIndex:randomIndex]];

[btn setImage:img forState:UIControlStateNormal];

[images removeObjectAtIndex:randomIndex];//或者[images removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:randomIndex]];

}

}

相关文章

网友评论

      本文标题:随机分配4个图片到四个按钮中

      本文链接:https://www.haomeiwen.com/subject/dwnelxtx.html