下面的代碼是從開始Iphone 4開發的搖動和烘焙示例的近似複製品。它在CMA加速加速= accelerometerData.acceleration之前缺少BOOL if語句;因爲我希望這個人可以隨意多次動搖和刷新結果。我有一個按鈕,可以完美地執行相同的代碼。當我運行代碼並搖動iPhone時,它會崩潰。我錯過了什麼會使這項工作?你不能像搖動方法一樣使用按鈕來運行相同的代碼嗎?從'開始Iphone 4開發'調整搖碼導致崩潰
example.h文件
#import <CoreMotion/CoreMotion.h>
#define kAccelerationThreshold 1.7
#define kUpdateInterval (1.0f/10.0f)
@interface exampleViewController : UIViewController {
CMMotionManager *motionManager;
}
@property (retain,nonatomic) CMMotionManager *motionManager;
@end
Example.m
@synthesize motionManager;
-(void)viewDidLoad {
self.motionManager = [[[CMMotionManager alloc] init] autorelease];
motionManager.accelerometerUpdateInterval = kUpdateInterval;
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[motionManager startAccelerometerUpdatesToQueue:queue
withHandler:
^(CMAccelerometerData *accelerometerData, NSError *error){
if (error) {
[motionManager stopAccelerometerUpdates];
} else {
CMAcceleration acceleration = accelerometerData.acceleration;
if (acceleration.x > kAccelerationThreshold
|| acceleration.y > kAccelerationThreshold
|| acceleration.z > kAccelerationThreshold){
// There is a bunch of other stuff here, but it all works using a button called shake....
example4.hidden = NO;
select1.text = first;
display.text = [[NSString alloc] initWithFormat: @"%@", first];
}
}
}];
}
- (void)dealloc
{
[super dealloc];
[motionManager release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.motionManager = nil;
}
@end
這幫助我實現了使用CoreMotion框架的搖動手勢,我無法在Google搜索 - > Stackoverflow上找到它。您應該添加它作爲答覆的一個帖子詢問如何做到這一點。 – Kunalxigxag 2014-01-24 06:22:35