How to change the position of thumbnail images randomly
I have 12 images in a thumbnail view and I want to change their position randomly on the click of a refresh button, so that my images are t开发者_JAVA技巧he same but their position changes in the thumbnail view. Can anyone help me with this.
use int r = arc4random()%12;
and change position of your buttons according to r.
for your width and height remain as it is , and change x and y position according to random number.
HI , I am using Unbutton instead of UIImage view because i have not different images.and for UI connivence, U have to replace Unbutton with imageViews ..enjoy it.
.h file : UIButton *b[12];
.m file :
- (void)viewDidLoad {
[super viewDidLoad];
float y = 10;
int x = 0;
int count = 0;
for (int i = 0 ; i < 12; i++)
{
count ++;
b[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b[i].frame = CGRectMake(x, y, 100, 100);
[b[i] setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
x = x + 105;
[self.view addSubview:b[i]];
if(count == 3)
{
count = 0;
x = 0;
y = y+ 105;
}
}
}
- (IBAction)btnClicked:(id)sender
{
int n = 12;
int temp;
for (int i = 0 ; i < 12 ; i++)
{
int r = arc4random()%n;
if(r != temp){
temp = r;
CGRect r1 = b[i].frame;
b[i].frame = b[temp].frame;
b[temp].frame = r1;
n--;
}
}
}
精彩评论