Permalink
Browse files

accelerometer added

  • Loading branch information...
1 parent 5b8ac19 commit 51cefd8f38d6c66a174e08785dcbbe9753c048fa @soid soid committed Apr 20, 2012
@@ -250,8 +250,12 @@
buildSettings = {
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "ch11/ch11-Prefix.pch";
+ GCC_VERSION = "";
INFOPLIST_FILE = "ch11/ch11-Info.plist";
+ IPHONEOS_DEPLOYMENT_TARGET = 5.0;
+ ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
+ SDKROOT = iphoneos;
WRAPPER_EXTENSION = app;
};
name = Debug;
@@ -261,8 +265,11 @@
buildSettings = {
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "ch11/ch11-Prefix.pch";
+ GCC_VERSION = "";
INFOPLIST_FILE = "ch11/ch11-Info.plist";
+ IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
+ SDKROOT = iphoneos;
WRAPPER_EXTENSION = app;
};
name = Release;
View
@@ -9,5 +9,7 @@
#import <UIKit/UIKit.h>
@interface ResultsController : UIViewController
+@property(assign) int peaksCount;
+@property (weak, nonatomic) IBOutlet UILabel *labelScore;
@end
View
@@ -14,6 +14,9 @@ @interface ResultsController ()
@implementation ResultsController
+@synthesize peaksCount;
+@synthesize labelScore;
+
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
@@ -27,10 +30,13 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
+
+ labelScore.text = [NSString stringWithFormat:@"Your Score: %d", peaksCount];
}
- (void)viewDidUnload
{
+ [self setLabelScore:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
@@ -7,14 +7,18 @@
//
#import <UIKit/UIKit.h>
+#import "ResultsController.h"
-@interface ShakeViewController : UIViewController {
+@interface ShakeViewController : UIViewController <UIAccelerometerDelegate> {
int count;
}
@property (strong, nonatomic) IBOutlet UILabel *timerLabel;
@property (strong, nonatomic) NSTimer *theTimer;
-
+@property(assign) int peaksCount;
+@property(assign) double lastPeakX, lastPeakY, lastPeakZ;
- (void)countDown:(id)sender;
@end
+
+
View
@@ -8,13 +8,17 @@
#import "ShakeViewController.h"
+#define kUpdateFrequency 60.0
+
@interface ShakeViewController ()
@end
@implementation ShakeViewController
@synthesize timerLabel;
@synthesize theTimer;
+@synthesize peaksCount;
+@synthesize lastPeakX, lastPeakY, lastPeakZ;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
@@ -30,17 +34,33 @@ - (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
count = 5;
- self.theTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
+ self.theTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
+
+ // setup accelerometer
+ [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / kUpdateFrequency];
+ [[UIAccelerometer sharedAccelerometer] setDelegate:self];
+
+ peaksCount = 0;
+ lastPeakX = lastPeakY = lastPeakZ = 0.1;
+}
+
+- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
+{
+ ResultsController *resultsVC = segue.destinationViewController;
+ resultsVC.peaksCount = peaksCount;
}
- (void)countDown:(id)sender {
count--;
- //NSLog(@"count=%d", count);
if(count==0)
{
[self.theTimer invalidate];
[self setTheTimer:nil];
- [self performSegueWithIdentifier:@"toResults" sender:self];
+
+ [self performSegueWithIdentifier:@"toResults" sender:self];
+
+ [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
+ [super viewDidUnload];
} else {
self.timerLabel.text = [NSString stringWithFormat:@"%d", count];
}
@@ -50,13 +70,26 @@ - (void)viewDidUnload
{
[self setTheTimer:nil];
[self setTimerLabel:nil];
- [super viewDidUnload];
// Release any retained subviews of the main view.
}
+// UIAccelerometerDelegate method, called when the device accelerates.
+-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
+{
+ // Update the accelerometer graph view
+ if (fabs(acceleration.x) > 1.5) {
+ if (acceleration.x * lastPeakX < 0) {
+ lastPeakX = acceleration.x;
+ peaksCount += 1;
+ }
+ }
+}
+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
+
+
@end
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="1.1" toolsVersion="2182" systemVersion="11D50d" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="1.0" toolsVersion="1938" systemVersion="11D50d" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
<dependencies>
- <deployment defaultVersion="1296" identifier="iOS"/>
+ <deployment defaultVersion="1280" identifier="iOS"/>
<development defaultVersion="4200" identifier="xcode"/>
- <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="1181"/>
+ <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="933"/>
</dependencies>
<scenes>
- <!--View Controller-->
<scene sceneID="5">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
@@ -67,7 +66,6 @@
</objects>
<point key="canvasLocation" x="81" y="187"/>
</scene>
- <!--Shake View Controller-->
<scene sceneID="XNg-PA-a9P">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="s9F-3E-8a5" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -100,9 +98,8 @@
</connections>
</viewController>
</objects>
- <point key="canvasLocation" x="496" y="187"/>
+ <point key="canvasLocation" x="679" y="187"/>
</scene>
- <!--Results Controller-->
<scene sceneID="Vay-jI-jky">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="Pgg-iX-Kvh" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -111,24 +108,45 @@
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
- <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Your Score Here." lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="10" id="s5v-qg-UmW">
- <rect key="frame" x="94" y="117" width="132" height="21"/>
+ <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Your Score: 0" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="10" id="s5v-qg-UmW">
+ <rect key="frame" x="20" y="103" width="280" height="55"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
- <fontDescription key="fontDescription" type="system" pointSize="17"/>
+ <fontDescription key="fontDescription" type="system" pointSize="25"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
+ <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="1DT-0f-Txa">
+ <rect key="frame" x="95" y="334" width="130" height="37"/>
+ <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+ <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
+ <state key="normal" title="Continue...">
+ <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
+ <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
+ </state>
+ <state key="highlighted">
+ <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+ </state>
+ <connections>
+ <segue destination="2" kind="modal" id="dRt-37-JdY"/>
+ </connections>
+ </button>
</subviews>
<color key="backgroundColor" cocoaTouchSystemColor="darkTextColor"/>
</view>
+ <connections>
+ <outlet property="labelScore" destination="s5v-qg-UmW" id="ovS-gm-ue6"/>
+ </connections>
</viewController>
</objects>
- <point key="canvasLocation" x="926" y="187"/>
+ <point key="canvasLocation" x="377" y="-400"/>
</scene>
</scenes>
<classes>
<class className="ResultsController" superclassName="UIViewController">
<source key="sourceIdentifier" type="project" relativePath="./Classes/ResultsController.h"/>
+ <relationships>
+ <relationship kind="outlet" name="labelScore" candidateClass="UILabel"/>
+ </relationships>
</class>
<class className="ShakeViewController" superclassName="UIViewController">
<source key="sourceIdentifier" type="project" relativePath="./Classes/ShakeViewController.h"/>

0 comments on commit 51cefd8

Please sign in to comment.