【问题标题】:how to detect and program around shakes for the iphone如何检测和编程 iPhone 的震动
【发布时间】:2010-11-23 08:28:37
【问题描述】:

我正在尝试在此页面上实现摇动“教程”,但我认为我遗漏了一些东西。我将他的加速度计功能复制到 myAppViewController.m 文件中,并在其中放置了一些 nslog,以查看当我使用模拟器“摇动”功能时它是否进入该功能。调试控制台中没有显示任何内容。

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

谁能解释我可能遗漏了什么?或者指点我一个教程?

我发现了这个,看起来很有希望,但我不知道如何“把它放在 UIView 中” How do I detect when someone shakes an 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 ");       
    }
}

【问题讨论】:

  • 是的,它在模拟器中工作。
  • 其实调用becomeFirstResponder是没有必要的。允许 viewController 响应运动事件所需的唯一操作是确保 viewController 可以成为 firstResponder。如文档中所述,UIView 将motionBegan 等消息向上传递到响应者链,最终将找到视图的控制器,只要canBecomeFirstResponder 返回YES。

标签: iphone iphone-sdk-3.0 delegates shake


【解决方案1】:

这是我的有效答案:

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

}

我只用模拟器测试过,它返回我:

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

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

我希望这会有所帮助,因为我花了 2 个小时来完成这项工作。

【讨论】:

    【解决方案2】:

    您应该绝对不使用自己的过滤器直接收听UIAccelerometer 来处理抖动事件。这是一项高功率操作,仅应由需要高加速度计采样率的应用程序使用。改用已添加到UIEvent 的新运动事件:

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

    就像触摸一样,运动事件将被传递给第一响应者,然后如果第一响应者没有响应,则沿着响应者链向上传播。 UIEvent 将具有类型 UIEventTypeMotion 和子类型 UIEventSubtypeMotionShake

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多