Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to detect and program around shakes for the iphone

I'm trying to implement the shake "tutorial" on this page, but I think I'm missing something. I copied his accelerometer function into myAppViewController.m file and put some nslogs in there to see if it even gets into the function when I use the simulators "shake" function. Nothing shows up in the debug console.

http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk

Can anyone explain what I might be missing? Or point me to a tutorial?

I found this, which looks promising, but I don't know how to "put it in a UIView" How do I detect when someone shakes an iPhone?


EDIT - now here's my working code because of the accepted answer's suggestion.

Here's my code to detect the shake gesture in 3.0 iphone sdk.

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"{motion ended event ");

    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"{shaken state ");

    }
    else {
        NSLog(@"{not shaken state ");       
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my answer that works:

//MainViewController.m

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(shake) 
                                 name:@"shake" object:nil];

    if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake)
        NSLog(@"motion Began");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(shake)
                                                 name:@"shake"
                                               object:nil];
    if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake)
        NSLog(@"motion Ended");
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(shake) 
                                                 name:@"shake" 
                                               object:nil];
    if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake)
        NSLog(@"motion Cancelled");
}

-(void)viewDidLoad {
    [super viewDidLoad];

    [self becomeFirstResponder];
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self resignFirstResponder]; 

}

I tested only with simulator and it returns me:

2010-06-22 12:40:48.799 Cocktails[14589:207] motion Began

2010-06-22 12:40:48.800 Cocktails[14589:207] motion Ended

I hope this help, because I loose 2 hours of doing this work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...