Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-04-2009, 03:40 PM   #2 (permalink)
NewiPhoneDeveloper
Registered Member
 
Join Date: Jul 2008
Posts: 459
Default

Quote:
Originally Posted by dsinc14 View Post
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!
NewiPhoneDeveloper is offline   Reply With Quote
 

» Advertisements
» Online Users: 743
12 members and 731 guests
Bertrand21, chiataytuday, Inupgames, kampftrinker, Macaret, mapiko, nidie, soonw29, srishtimehta, superblooddrainer, wodanbrlam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,825
Threads: 89,207
Posts: 380,637
Top Poster: BrianSlick (7,129)
Welcome to our newest member, srishtimehta
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.