我使用的PhoneGap 0.8和我想检测基本摇动手势。我发现两个相关的片段:

http://groups.google.com/group/phonegap/browse_thread /线程/ 25178468b8eb9e9f

http://phonegap.pbworks.com/Handling-Shake-Events

他们都不是为我工作。不幸的是,文件似乎有点过时和PhoneGap的API参考只是一堆的测试。我知道这是可以做到我只是没有一个已知的良好起点。

如果任何人都可以提供为PhoneGap的,他们知道他们的作品,我将非常感激抖动检测一小段程式码。感谢您的时间!

有帮助吗?

解决方案

您第一链路使用PhoneGap.Gesture,其据我可以看到不0.8(至少iphone)存在。这是一个相当最近的文章,但这样可能最近添加?我还没有使用任何以后的修订版0.8

我已通过“”延伸的PhoneGap如下加入到PhoneGap的应用摇动手势支持。但第一警告:

<强>苹果都视为PhoneGap的0.8作为用于提交的应用程序商店可以接受的。由于这是本质上的延伸部(虽然非常简单),以0.8的PhoneGap可能存在排斥的风险。该应用程序我开发这个通过审批程序还没有走,所以我不能进一步建议任何这一点。

另外要注意,这仅适用于OS 3.0+由于使用新的API UIEventSubtypeMotionShake

此外,这是iPhone特定。如果你想的PhoneGap的跨平台能力,这个答案可能不适合你,除非你是舒服实现对其他平台的本地端修改(机器人等)。在这种情况下,它可能是更好的只是等待支持此对iPhone的正式发布的PhoneGap

下面的代码添加到PhoneGapViewController.m

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (UIEventSubtypeMotionShake == motion)
    {
        [webView stringByEvaluatingJavaScriptFromString:@"navigator.myext.onShakeGesture();"];
    }
}

然后添加为一个JavaScript模块myext.js

/* We need PhoneGap.js to be loaded to work properly before we can initialise so wait for it here */
(function() 
{
    var timer = setInterval(function() 
    {
        if (typeof(PhoneGap == 'object'))
        {
            clearInterval(timer);
            myExtInit();
        }
    }, 1);
})();

function MyExt()
{
    // initialization
    this.callbacks = {
        onShakeGesture: []
    };
}

/* Called by ObjectiveC side when a shake gesture is detected */
MyExt.prototype.onShakeGesture = function()
{
    for (var i = 0; i < this.callbacks.onShakeGesture.length; i++) 
    {
        var f = this.callbacks.onShakeGesture[i];
        f();
    }
};

MyExt.prototype.addShakeGestureListener = function(shakeCallback)
{
    if (typeof(shakeCallback) == 'function')
    {
        this.callbacks.onShakeGesture.push(shakeCallback)
    }
};

function myExtInit()
{
    // Init phonegap extension specific objects using PhoneGap.addConstructor
    PhoneGap.addConstructor(function() {
        if (typeof navigator.myext == "undefined") navigator.myext = new MyExt();
    });
}

现在在HTML内容<head>部分myext.js只是链接后phonegap.js

<script type="text/javascript" charset="utf-8" src="myext.js"/>

和从脚本中使用(例如导致警报,但你需要做什么):

function watchShake() {
  var cb = function(){
    navigator.notification.alert("Shake it!");
  };
    navigator.myext.addShakeGestureListener(cb);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top