我如何检测有人摇动iPhone的时间? [英] How do I detect when someone shakes an iPhone?

查看:103
本文介绍了我如何检测有人摇动iPhone的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有人动摇iPhone时,我想做出反应。我并不特别在意它们是如何动摇它的,只是它在瞬间挥动了一瞬间。有谁知道如何检测这个?

解决方案

在3.0中,现在有一种更简单的方法 - 挂钩新的运动事件。 / p>

主要技巧是你需要一些UIView(不是UIViewController)作为firstResponder接收震动事件消息。以下是您可以在任何UIView中使用以获取震动事件的代码:

  @implementation ShakingView 

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.subtype == UIEventSubtypeMotionShake)
{
//在此输入代码处理摇动
}

if([super respondsToSelector:@selector(motionEnded:withEvent :)])
[super motionEnded:motion withEvent:event];
}

- (BOOL)canBecomeFirstResponder
{return YES; }

@end

您可以轻松转换任何UIView(甚至系统视图) )只需通过仅使用这些方法对视图进行子类化(然后在IB中选择此新类型而不是基类型,或在分配视图时使用它)来获取可以获取抖动事件的视图。



在视图控制器中,您希望将此视图设置为第一响应者:

   - ( void)viewWillAppear:(BOOL)动画
{
[shakeView becomeFirstResponder];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)动画
{
[shakeView resignFirstResponder];
[super viewWillDisappear:animated];
}

如果您有其他视图成为用户的第一响应者,请不要忘记操作(如搜索栏或文本输入字段),当另一个视图停止时,您还需要恢复摇动视图第一响应者状态!



即使将applicationSupportsShakeToEdit设置为NO,此方法仍然有效。


I want to react when somebody shakes the iPhone. I don't particularly care how they shake it, just that it was waved vigorously about for a split second. Does anyone know how to detect this?

解决方案

In 3.0, there's now an easier way - hook into the new motion events.

The main trick is that you need to have some UIView (not UIViewController) that you want as firstResponder to receive the shake event messages. Here's the code that you can use in any UIView to get shake events:

@implementation ShakingView

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        // Put in code here to handle shake
    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

- (BOOL)canBecomeFirstResponder
{ return YES; }

@end

You can easily transform any UIView (even system views) into a view that can get the shake event simply by subclassing the view with only these methods (and then selecting this new type instead of the base type in IB, or using it when allocating a view).

In the view controller, you want to set this view to become first responder:

- (void) viewWillAppear:(BOOL)animated
{
    [shakeView becomeFirstResponder];
    [super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
    [shakeView resignFirstResponder];
    [super viewWillDisappear:animated];
}

Don't forget that if you have other views that become first responder from user actions (like a search bar or text entry field) you'll also need to restore the shaking view first responder status when the other view resigns!

This method works even if you set applicationSupportsShakeToEdit to NO.

这篇关于我如何检测有人摇动iPhone的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆