Home / iPad

Controlling the View

Select the DeepThoughtsViewController.m file so that it appears in the Text editor, and insert the code in bold in Listing 1-2. (The code that's not in bold is supplied by the View-based Application template, except the nonbolded #pragma mark markers, which you added earlier in this tutorial, in "Marking code sections in the view controller.")

Listing 2-2: DeepThoughtsViewController.m
#import "DeepThoughtsViewController.h"
#import "Constants.h"

@implementation DeepThoughtsViewController
@synthesize speed, imageView;
@synthesize fallingWords;
#pragma mark -
#pragma mark View life cycle
/*
// The designated initializer. Override to perform setup
	    that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil
	    bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil
	    bundle:nibBundleOrNil]) {
	// Custom initialization
    }
    return self;
}
*/
/*
// Implement loadView to create a view hierarchy
	    programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after
	    loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/
- (void)viewDidLoad {

    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self
	    selector:@selector(onTimer) userInfo:nil
	    repeats:YES];
    if (![[NSUserDefaults standardUserDefaults]
	    objectForKey:kWordsOfWisdom]) {
	[[NSUserDefaults standardUserDefaults]
	    setObject:@"Peace Love Groovy Music"
	    forKey:kWordsOfWisdom];
    fallingWords = @"Peace Love Groovy Music";
    }
    else {
	fallingWords = [[NSUserDefaults standardUserDefaults]
	    stringForKey:kWordsOfWisdom];
    }
    if (![[NSUserDefaults standardUserDefaults]
	    objectForKey:kSpeed] ){
	[[NSUserDefaults standardUserDefaults]setDouble:10.0
	    forKey:kSpeed];
	speed = kMaxSpeed-10.0;}
    else {
	speed = kMaxSpeed-[[NSUserDefaults
	    standardUserDefaults] doubleForKey:kSpeed] ;
    }
}

#pragma mark -
#pragma mark Animation

- (void)onTimer{

    UILabel *fallingImageView = [[UILabel alloc]
	    initWithFrame:CGRectMake(0, 0, 100, 30)];
    fallingImageView.text = fallingWords;
    fallingImageView.textColor = [UIColor purpleColor];
    fallingImageView.font = [UIFont systemFontOfSize:30];
    fallingImageView.backgroundColor = [UIColor
	    clearColor];

    fallingImageView.adjustsFontSizeToFitWidth = YES;

    int startX = round(random() % 400);
    int endX = round(random() % 400);
    //speed of falling
    double randomSpeed = (1/round(random() % 100) +1)
	    *speed;
    // image size;
    double scaleH = (1/round(random() % 100) +1) *60;
    double scaleW = (1/round(random() % 100) +1) *200;

    CGRect startImageFrame = fallingImageView.frame;
    CGRect endImageFrame = fallingImageView.frame;
    startImageFrame.origin = CGPointMake(startX, -100);
    endImageFrame = CGRectMake(endX, self.view.frame.size.
	    height,scaleW, scaleH);
    fallingImageView.frame = startImageFrame;
    fallingImageView.alpha = .75;
    [self.view addSubview:fallingImageView];

[UIView animateWithDuration:randomSpeed
		animations:^ {
		  [UIView setAnimationDelegate:self];
		  fallingImageView.frame =
	    CGRectMake(endX, self.view.frame.size.height,
	    scaleW, scaleH);
		}
		completion:^(BOOL finished){
		  [fallingImageView
	    removeFromSuperview];
		  [fallingImageView release];
		}];
}

#pragma mark -
#pragma mark Controls

- (IBAction)settings {

}
#pragma mark -
#pragma mark Orientation

// Override to allow orientations other than the default
	    portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfac
	    eOrientation)interfaceOrientation {
    return YES;
}

#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
	    // Releases the view if it doesn't have a
	    superview.
    [super didReceiveMemoryWarning];

	    // Release any cached data, images, etc that
	    aren't in use.
}
- (void)viewDidUnload {
	    // Release any retained subviews of the main view.
	    // e.g. self.myOutlet = nil;
    self.imageView = nil;
    self.fallingWords = nil;

}

- (void)dealloc {
    self.imageView = nil;
    self.fallingWords = nil;
    [super dealloc];
}

@end

That's a lot to swallow at once, but I explain how all this works in the rest of this tutorial.

The first statement you add imports the Constants.h file:

#import "Constants.h"

You can now use the keys you set up in "Adding a Constants.h file" in this tutorial with NSUserDefaults in the subsequent code to retrieve the user settings.

Although the @property declarations way back in Listing 10-1 tell the compiler that there are accessor methods, these methods still have to be created. Fortunately, Objective-C will create these accessor methods for you whenever you include an @synthesize statement - the next bolded item in Listing 1-2:

@synthesize speed, imageView;
@synthesize fallingWords;

The @synthesize statements tell the compiler to create accessor methods for you - one for each @property declaration (speed, imageView, and fallingWords).

At the end of the bolded code you add in Listing 1-2 is a new #pragma mark section titled Controls that includes the placeholder settings method for connecting the Light Info button to the view controller:

#pragma mark -
#pragma mark Controls

- (IBAction)settings {

}

This is the action method using the IBAction qualifier. You use Interface Builder to specify that when the user taps the Light Info button, the target is the DeepThoughtsViewController object, and the method to invoke is settings.

[Previous] [Contents] [Next]