如何检测和编程为iPhone摇晃

Neo*_*o42 6 iphone delegates shake iphone-sdk-3.0

我正试图在这个页面上实现摇动"教程",但我想我错过了一些东西.我将他的加速计功能复制到myAppViewController.m文件中并在其中放入一些nslog,以查看当我使用模拟器"摇动"功能时它是否进入该功能.调试控制台中没有显示任何内容.

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

任何人都可以解释我可能会缺少什么?还是指点教程?

我发现这看起来很有希望,但我不知道如何"把它放在一个UIView" 我如何检测有人摇动iPhone?


编辑 - 现在这是我的工作代码,因为接受了答案的建议.

这是我在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 ");       
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

这是我的答案:

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

}
Run Code Online (Sandbox Code Playgroud)

我只用模拟器进行测试,它返回给我:

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

2010-06-22 12:40:48.800 Cocktails[14589:207] motion Ended
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助,因为我放松了2个小时做这项工作.


小智 4

绝对不UIAccelerometer应该直接用自己的过滤来监听来处理震动事件。这是一种高功率操作,只能由需要高加速度计采样率的应用程序使用。使用已添加到的新运动事件UIEvent

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

就像触摸一样,运动事件将被传递给第一响应者,如果第一响应者没有响应,则沿着响应者链向上移动。将UIEvent具有 typeUIEventTypeMotion和 subtype UIEventSubtypeMotionShake