Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
kennedy otis
3,570 PointsRandom Background images
Hi guys. With regard to the ios 7 crystall ball app, I am trying to generate random background images instead of the animation.I have used the same concept which was used to generate random predictions but my background changes only once. This is my code http://pastebin.com/eqYbp2dS The background images changes only once and my goal is for it to changing randomly everytime i touch the iphone screen.
5 Answers
Amit Bijlani
Treehouse Guest TeacherMost of your code is fine except what's going on in the randomBackground method. The following line:
[self.view insertSubview:backgroundImage atIndex:0];
Inserts a new view behind all the existing views. You should not have to continually insert new views. All you need to do is have an ImageView property which should be an IBOutlet to an image view in your Storyboard. Then within your randomBackground method you can do the following:
-(void)randomBackground
{
NSString *imgName = [self.quoteModel randomImages];
UIImage *randomImage = [UIImage imageNamed:imgName];
self.backgroundImage.image = randomImage;
}
kennedy otis
3,570 PointsIn the last line where you say self.backgroundImage.image = randomImage; is confusing me a bit. I have created an IBOutlet in my ViewController header file like @property (strong, nonatomic) IBOutlet UIImageView *image; and linked it to the storyboard and have a default image before clicked and ensured the UIImageView is at index:0. Hope am on the right track. Then my randomBackground looks like so:
-(void)randomBackground
{
NSString *imgName = [self.quoteModel randomImages];
UIImage *randomImage = [UIImage imageNamed:imgName];
}
So where is that self.backgroundImage coming from or what is it? As for the last .image I assume is the IBOutlet.Would really appreciate if can get clarifications here.
Amit Bijlani
Treehouse Guest TeacherWhere you named your IBOutlet image, I named it backgroundImage. You need that last line to change the image of your image view.
kennedy otis
3,570 PointsThanks now its working but I have tried to reuse the same method under the shake motion but the background is not changing. My goal is to change the background for both touch and shaking.
-(void)randomBackground { NSString *imgName = [self.quoteModel randomImages];
UIImage *randomImage = [UIImage imageNamed:imgName];
self.backgroundImage.image = randomImage;
}
pragma mark - Motion events
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { self.quoteLabel.text = nil; [self backgroundImage]; }
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { [self backgroundImage]; self.quoteLabel.text = [self.quoteModel randomQuote]; }
}
Amit Bijlani
Treehouse Guest TeacherYou should be calling the randBackground method in your motionEnded method: [self randomBackground];
kennedy otis
3,570 PointsThis might sound strange but Its not working. Here is how i do it
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { [self backgroundImage]; self.quoteLabel.text = [self.quoteModel randomQuote]; }
}
Amit Bijlani
Treehouse Guest TeacherYou are calling backgroundImage which is the property but the method is named randomBackground.
kennedy otis
3,570 PointsRight..I saw my problem.Thanks for that. Now its working like a charm. :)