Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nananee

macrumors newbie
Original poster
Oct 4, 2010
2
0
Hi all,

I am extremely new to iPhone development and I was assigned the 'easy' task of creating a pulsating color background. Basically, I need the screen to pulsate different colors. I understand the logic behind it - I need a timer that changes the color every second, and I need to generate random colors to assign the background to. This is what I have so far..

UIColorRandom.h
Code:
#import <Foundation/Foundation.h>
#import <UIKit/UIkit.h> 

@interface UIColorRandom {
}
+(UIColor *)randomColor;

@end
UIColorRandom.m
Code:
#import "UIColorRandom.h"


@implementation UIColorRandom
+(UIColor *)randomColor
{
	static BOOL seeded = NO; // tells us if it is the first time through this method
	if (!seeded) {	// after seeding has been done 
		seeded = YES; // seeded will change to YES so that it states it is not the first time 
		srandom(time(NULL)); 
	}
	
	
	{		
		// generate random floats for rgb to make random colors values from 0.0 - 1.0
		CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; 
		CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
		CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
		return[UIColor colorWithRed:red green:green blue:blue alpha:1.0];
	}	
}
@end
ColorPulseViewController.m
Code:
#import "ColorPulseViewController.h"
#import "UIColorRandom.h"

@implementation ColorPulseViewController
-(void)loadView
{
[NSTimer scheduledTimerWithTimeInterval:1
		 target:self 
		 selector:@selector(pulseColor) 
		 userInfo:nil 
		 repeats:YES];
}

-(void)pulseColor 
{
	UIColor *randColor = [UIColorRandom randomColor];
	self.view.backgroundColor = randColor; 
}
I don't get any error/warning messages during the build but it continues to crash.. this is the message that I get in the debugger window.

2010-10-04 14:23:45.976 ColorPulse[15819:207] ******* Accessibility Status Changed: On
2010-10-04 14:23:46.001 ColorPulse[15819:207] ********** Loading AX for: com.yourcompany.ColorPulse ************
2010-10-04 14:23:47.046 ColorPulse[15819:207] *** NSInvocation: warning: object 0x30a0 of class 'UIColorRandom' does not implement methodSignatureForSelector: -- trouble ahead
2010-10-04 14:23:47.047 ColorPulse[15819:207] *** NSInvocation: warning: object 0x30a0 of class 'UIColorRandom' does not implement doesNotRecognizeSelector: -- abort

I am extremely new to this.. and by new I mean I have just been introduced to this for less than a week now and I am suppose to learn this on my own. I have been doing all the tutorials but I can't seem to understand what is going on. I know this is a really easy task and that is why I am extremely frustrated I can't get it to work. Any help is greatly appreciated! Thanks !
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Don't start your timer in loadView. Do this in viewDidLoad or viewWillAppear. And don't override loadView unless you know why you're doing it.
 

(marc)

macrumors 6502a
Sep 15, 2010
724
2
the woods
Look at your interface declaration for UIColorRandom. You have to make UIColorRandom a subclass of NSObject. :)


EDIT: The random color thing is actually well suited for an Objective-C category. For example:

Code:
#import <UIKit/UIKit.h>


@interface UIColor (UIColorAdditions)

+ (UIColor *)randomColor;

@end
Code:
#import "UIColorAdditions.h"


CGFloat randomFloat() {
	return ((CGFloat)random()) / ((CGFloat)RAND_MAX);
}


@implementation UIColor (UIColorAdditions)


+ (void)initialize {
	srand(time(NULL));
}


+ (UIColor *)randomColor {
	return [UIColor colorWithRed:randomFloat()
				       green:randomFloat()
					 blue:randomFloat()
				       alpha:1.0];
}
										   

@end



EDIT 2: I don't know what you mean by "plusating", but to me, it doesn't sound so much like setting random colors, but like smoothly stepping through the color palette. To do so, you would add a CGFloat instance variable named "hue" to your view controller. Then, modify pulseColor:
Code:
- (void)pulseColor {
	hue = fmodf(hue + 0.01, 1.0);
	[self.view setBackgroundColor:[UIColor colorWithHue:hue
                                    saturation:1.0
                                   brightness:1.0
                                         alpha:1.0]];
}
 

nananee

macrumors newbie
Original poster
Oct 4, 2010
2
0
Wow, that was a fairly simple fix. By 'pulsating' I just meant that the background color would change at a defined frequency being every second, so what you gave me initially worked like a charm since all I needed was to generate random colors to display.

Also, for future reference when would I need to initiate something in the following:

loadView
viewDidLoad
awakeFromNib

I just need a more pragmatic approach because clearly right now I am guessing and checking because I don't fully understand the differences.

This is just a threshold for what I want to actually accomplish. Ultimately I want the background color to change with each step an individual takes. I am assuming I am going to have to figure out how to use the accelerometers as pedometers, or find a way to integrate Nike+ data =D

It never ends..!! Any thoughts to where to begin?

Thanks again!

P.S Forgive my noobiness, you guys are great. I'll make sure that even though I am a noob I don't look like one in my posts =P
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Read the View Controller Programming Guide, The Human Interface Guidelines, and the guides listed under getting started.

Then get books on Objective-C and iPhone programming and work through them.

You could also read the sticky at the top of the forum.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.