how to detect and program around shakes for the iphone

房东的猫 提交于 2019-11-27 16:42:35

问题


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 ");       
    }
}

回答1:


You should absolutely not be listening to UIAccelerometer directly with your own filtering to handle shake events. That is a high-power operation and should only be used by apps that need a high accelerometer sampling rate. Use the new motion events instead which have been added to UIEvent:

http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/uid/TP40007072-CH9-SW24

Just like touches, a motion event will be delivered to the first responder, then travel up the responder chain if the first responder does not respond. The UIEvent will have type UIEventTypeMotion and subtype UIEventSubtypeMotionShake.




回答2:


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.



来源:https://stackoverflow.com/questions/1340492/how-to-detect-and-program-around-shakes-for-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!