Quote:
Originally Posted by dsinc14
hello i am making an app that would involve randomizing a picture, but im not sure what to do.
I wanted it to be able to click anywhere on the screen for another random image.
If anyone knows the code plzzz leave a reply thank you.
|
Well, let me try to put together a basic workflow for your problem:
1) Create an array and add names of your images to it.
2) Add a UIImageView element with the size of your screen to the view.
3) Implement a method to generate a random number, like:
Code:
- (int)getRandomNumber:(int)from to:(int)to {
return (int)from + arc4random() % (to-from+1);
}
3) Implement the touches ended method, like:
Code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
//detect the touch
if (CGRectContainsPoint([nameOfYourUIImageView frame], [touch locationInView:self.view])) {
//set the new random image to your UIImageView
nameOfYourUIImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[nameOfYourArray objectAtIndex:[self getRandomNumber:0 to:[nameOfYourArray count]-1]]] ofType:@"png"]];
//right above we're calling the getRandomNumber method, we previously implemented. As expected it gets us a random number between 0 (arrays start at 0) and the maximum items in that array. We're also subtracting 1 from the array count (max items in the array), in order to prevent the app from crashing. To make that clear: arrays start from 0, but if you call [nameOfYourArray count] it gets you the number of items in your array and of course starts counting at 1.
}
I hope that gives you a basic idea and helps you to get started. Just browse this forum for more detailed examples on each of the tasks I described. Good luck!