Skip to content

Commit

Permalink
Add MultipleAccelerometer example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Katzian committed Jun 15, 2010
1 parent 9c33dc4 commit 2336e72
Show file tree
Hide file tree
Showing 17 changed files with 1,357 additions and 0 deletions.
21 changes: 21 additions & 0 deletions MultipleAccelerometer/Classes/Accelerometer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Accelerometer.h
//
// Created by Andreas Katzian on 15.06.10.
// Copyright 2010 Blackwhale GmbH. All rights reserved.
//


@interface Accelerometer : NSObject <UIAccelerometerDelegate> {

@private
NSMutableArray *delegates; //array of subscribed delegates

}

+ (Accelerometer*) sharedInstance;

- (void) addDelegate:(id<UIAccelerometerDelegate>) delegate;
- (void) removeDelegate:(id<UIAccelerometerDelegate>) delegate;

@end
129 changes: 129 additions & 0 deletions MultipleAccelerometer/Classes/Accelerometer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Accelerometer.m
//
// Created by Andreas Katzian on 15.06.10.
// Copyright 2010 Blackwhale GmbH. All rights reserved.
//
// The singelton pattern which is used was thankfully described at
// http://www.duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/
//

#import "Accelerometer.h"

// Anonymus category for private properties
// and methods.
@interface Accelerometer ()

@property(nonatomic,retain) NSMutableArray *delegates; //array of subscribed delegates

@end


static Accelerometer* sharedInstance_ = nil; //the shared instance


// Class implementation
@implementation Accelerometer

@synthesize delegates;


#pragma mark -
#pragma mark Delegates management

// Subscribe a new delegate.
- (void) addDelegate:(id<UIAccelerometerDelegate>) delegate
{
[self.delegates addObject:delegate];
}

// Remove a delegate.
- (void) removeDelegate:(id<UIAccelerometerDelegate>) delegate
{
[self.delegates removeObject:delegate];
}


#pragma mark -
#pragma mark UIAccelerometerDelegate delegate

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
@synchronized(sharedInstance_)
{
//Notify all delegates of acceleration event
for(id<UIAccelerometerDelegate> delegate in self.delegates)
{
[delegate accelerometer:accelerometer didAccelerate:acceleration];
}
}
}


#pragma mark -
#pragma mark Singelton methods

// Get the shared instance of this singelton.
+ (Accelerometer*) sharedInstance
{
@synchronized(self)
{
[[self alloc] init];
}

return sharedInstance_;
}

// Overwrite allocWithZone to be sure to
// allocate memory only once.
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedInstance_ == nil) {
sharedInstance_ = [super allocWithZone:zone];

//additional initialization
sharedInstance_.delegates = [[NSMutableArray alloc] init];
[UIAccelerometer sharedAccelerometer].delegate = sharedInstance_;

return sharedInstance_;
}
}

return nil;
}

// Nobody should be able to copy
// the shared instance.
- (id)copyWithZone:(NSZone *)zone
{
return self;
}

// Retaining the shared instance should not
// effect the retain count.
- (id)retain
{
return self;
}

// Releasing the shared instance should not
// effect the retain count.
- (void)release
{
}

// Auto-releasing the shared instance should not
// effect the retain count.
- (id)autorelease
{
return self;
}

// Retain count should not go to zero.
- (NSUInteger)retainCount
{
return NSUIntegerMax;
}

@end
22 changes: 22 additions & 0 deletions MultipleAccelerometer/Classes/MultipleAccelerometerAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// MultipleAccelerometerAppDelegate.h
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright Blackwhale GmbH 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@class MultipleAccelerometerViewController;

@interface MultipleAccelerometerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MultipleAccelerometerViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MultipleAccelerometerViewController *viewController;

@end

98 changes: 98 additions & 0 deletions MultipleAccelerometer/Classes/MultipleAccelerometerAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// MultipleAccelerometerAppDelegate.m
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright Blackwhale GmbH 2010. All rights reserved.
//

#import "MultipleAccelerometerAppDelegate.h"
#import "MultipleAccelerometerViewController.h"
#import "Accelerometer.h"
#import "ObserverOne.h"
#import "ObserverTwo.h"

@implementation MultipleAccelerometerAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

ObserverOne *one = [[[ObserverOne alloc] init] autorelease];
ObserverTwo *two = [[[ObserverTwo alloc] init] autorelease];

[[Accelerometer sharedInstance] addDelegate:one];
[[Accelerometer sharedInstance] addDelegate:two];


// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}


- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}


- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// MultipleAccelerometerViewController.h
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright Blackwhale GmbH 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MultipleAccelerometerViewController : UIViewController {

}

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// MultipleAccelerometerViewController.m
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright Blackwhale GmbH 2010. All rights reserved.
//

#import "MultipleAccelerometerViewController.h"

@implementation MultipleAccelerometerViewController



/*
// 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];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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;
}


- (void)dealloc {
[super dealloc];
}

@end
16 changes: 16 additions & 0 deletions MultipleAccelerometer/Classes/ObserverOne.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// ObserverOne.h
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright 2010 Blackwhale GmbH. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface ObserverOne : NSObject <UIAccelerometerDelegate> {

}

@end
23 changes: 23 additions & 0 deletions MultipleAccelerometer/Classes/ObserverOne.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ObserverOne.m
// MultipleAccelerometer
//
// Created by Andreas Katzian on 15.06.10.
// Copyright 2010 Blackwhale GmbH. All rights reserved.
//

#import "ObserverOne.h"


@implementation ObserverOne

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
NSLog(@"Observer one was notified about acceleration (x: %f y: %f z: %f).",
acceleration.x,
acceleration.y,
acceleration.z);
}


@end
Loading

0 comments on commit 2336e72

Please sign in to comment.