Take the 2-minute tour ×

i m looking to create multiple dots in a screen at random coordinates and than connect the dot by its nearest dot.

or lets say, we have an array of 10 (x,y positions of the view) than ?

for (int i=0; i<MATRIX_SIZE; i++) {
    for (int j=0; j<MATRIX_SIZE; j++) {
        UIImage *dotImage = [UIImage imageNamed:@"dot_off.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:dotImage
                                                   highlightedImage:[UIImage imageNamed:@"Icon.png"]];
        imageView.frame = CGRectMake(0, 0, dotImage.size.width, dotImage.size.height);
        imageView.userInteractionEnabled = YES;
        imageView.tag = j*MATRIX_SIZE + i + 1;
        [self.view addSubview:imageView];

Guide me to draw or place same image at random positions.


share|improve this question
To me it's unclear what connect the dot by its nearest dot means? –  Krumelur Feb 3 at 12:10
Check the image : depositphotos.com/2991435/… first i need to draw/set images randomly like this and than draw a line between 2 dost. –  iscavengers Feb 3 at 12:15
You don't want to use UIView and images for that. Instead, learn about Core Graphics or SpriteKit on iOS to get some performance. –  Krumelur Feb 3 at 12:27
any reference?? –  iscavengers Feb 3 at 12:32
add comment

2 Answers

Use time interval to call arc4random() method, to get random values based on random values display the images.

like int randomNumber = (arc4random()%(toNumber-fromNumber))+fromNumber;

call the method with minimum and maximum values for screen.

share|improve this answer
add comment

Randomly generate the origin of the image view. Assuming you want the does randomly placed anywhere within self.view:

// Set the maximum x and y based on the parent view size
CGFloat maximumX = self.view.frame.size.width;
CGFloat maximumY = self.view.frame.size.height;

Inside the loop, change the imageView.frame = ... to:

float randomX = ((float)rand() / RAND_MAX) * maximumX;
float randomY = ((float)rand() / RAND_MAX) * maximumY;
imageView.frame = CGRectMake(randomX, randomY, dotImage.size.width, dotImage.size.height);

If you want the entire image to be within self.view, subtract half the width and height from randomX and randomY, respectively.

share|improve this answer
add comment

Your Answer


