我正在为 iPad 构建一款游戏作为网络应用程序。它结合了摇动动作来触发其中一个屏幕的事件,但是,由于它还具有信息收集表单,因此有时摇动动作会触发恼人的“摇动以撤消”对话框。当用户进行摇动操作时,用户输入的表单输入甚至不再存在于 DOM 中,因此在我看来,撤消功能不应该被触发,但不幸的是它确实如此。无法将其包装在本机包装器(PhoneGap 或其他)中,因此我想知道是否有人知道是否可以通过 CSS 或 JavaScript 为特定网页/应用程序禁用此功能?
谢谢。
I'm building a game as a webapp for the iPad. It incorporates a shake action to trigger an event for one of the screens, but, since it also has a information collection form, sometimes that shake action triggers the annoying "Shake to Undo" dialog. The form input the user has typed in don't even exist in the DOM anymore when the user reaches the shake action, so in my mind the Undo function shouldn't be triggered, but unfortunately it is. There is no way this can be wrapped in a native wrapper (PhoneGap or other), so I was wondering if anyone knows if it's at all possible to disable this functionality for a specific web page/app, through CSS or JavaScript?
Thanks.
发布评论
评论(9)
本博客描述了使用 DeviceMotionEvent 侦听器的非常简单的震动检测:
http://www.jeffreyharrell.com/blog/2010/11/creating-a-shake-event-in-mobile-safari/
尝试以下代码以禁用撤消事件发生:
有我能想到的另一件事是在完成后将文本输入转换为只读项目。据推测,只读输入字段无法使用撤消功能,尽管我还没有测试过。奇怪的是,即使输入不再是 DOM 的一部分,该功能仍然会触发。
我不确定是否可以通过 CSS 中的 -webkit- 属性来防止摇动操作。以下是 webkit 特定 CSS 属性的列表(可能不包含 100% 可访问的属性)。
http://qooxdoo.org/documentation/general/webkit_css_styles
This blog describes a very simple shake detection using the DeviceMotionEvent listener:
http://www.jeffreyharrell.com/blog/2010/11/creating-a-shake-event-in-mobile-safari/
Try the following code to disable to undo event happening:
There is one other thing I can think of which would be to turn the text input into a readonly item after you are done with it. Presumably, a read only input field cannot make use of the undo function, though I haven't tested it. It's odd that the feature fires even after the input is no longer part of the DOM.
I'm not sure if preventing a shake action can be done through the -webkit- properties in CSS. Here is a list of webkit specific CSS properties (which may not contain 100% of the properties accessible).
http://qooxdoo.org/documentation/general/webkit_css_styles
对于 PhoneGap,我刚刚插入了这一行:
[UIApplication共享应用程序].applicationSupportsShakeToEdit = NO;
在 webViewDidFinishLoad 类中,它对我来说创造了奇迹。
只需转到 MainViewController.m 并将 webViewDidFinishLoad 更改为如下所示:
For PhoneGap I just inserted this line:
[UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;
in the webViewDidFinishLoad class and it worked wonders for me.
Just go to the MainViewController.m and change webViewDidFinishLoad to look like this:
我最近在开发一款游戏时遇到了这个问题,其中用户使用 WebSockets 配对设备。不幸的是,上面提到的解决方案都不起作用。
简而言之,该游戏涉及用户通过手机上的表单订阅频道。一旦订阅,他们就会摇动设备,然后一个场景就会在他们的桌面上播放。问题是,每当有人摇动 iOS 设备时,就会弹出“撤消输入”警报。
这是我针对此问题找到的仅有的两个解决方案。
防止您的输入失去焦点。如果输入是表单的一部分,这将要求您阻止表单提交。您还必须侦听任何锚点上的“touchstart”而不是“单击”,并防止 touchstart 事件传播。这将防止该锚点获得焦点以及您的输入失去焦点。这种方法肯定有一些缺点。
向窗口添加“keydown”事件处理程序并阻止该事件传播。这可以防止任何值插入到 iOS 设备上的输入中。我还必须创建一个将键码映射到正确字符的对象。这是我最终选择的路线,因为我输入的所有内容都是 6 个字符的代码,所以我只需要数字。如果你需要的不仅仅是数字,这可能会很痛苦。按下按键时,通过键码捕获该字符并设置输入的值。这会欺骗 iOS 认为没有任何值通过键盘插入到输入中,因此它无法撤消任何操作。
请记住,阻止 keydown 事件传播并不会阻止在原生 Android 浏览器上进行输入,因此它会正常运行。但这没关系,因为这不是 Android 问题。您可以通过侦听输入本身的输入事件并在收到此事件时删除 keydown 侦听器来防止插入重复值。
I recently ran into this issue while developing a game where a user pairs devices using WebSockets. Unfortunately none of the solutions mentioned above work.
In short, the game involved a user subscribing to a channel via a form on their phone. Once subscribed they would shake the device and a scenario would play out on their desktop. The problem was that any time someone shook an iOS device the "undo typing" alert would pop up.
Here are the only two solutions that I've found for this issue.
Prevent your input from ever losing focus. This will require you to prevent form submission if the input is part of a form. You must also listen for "touchstart" instead of "click" on any anchors and prevent the touchstart event from propagating. This will prevent that anchor from gaining focus and your input from losing focus. This approach has some disadvantages for sure.
Add a "keydown" event handler to the window and prevent the event from propagating. This prevents any values from getting inserted into the input on iOS devices. I also had to create an object that mapped the keycodes to the proper character. This is the route I ended going because all I needed in my input was a 6 character code so I only needed numbers. If you need more than just numbers this may be a pain in the ass. On key down, snag that character via the keycode and set the value of the input. This tricks iOS into thinking that no value was ever inserted into the input via the keyboard so there is nothing for it to undo.
Keep in mind preventing the keydown event from propagating does not prevent input on the stock android browser, so it will just function normally. But that is OK since this is not an android issue. You can prevent duplicate values from getting inserted by listening for the input event on the input itself and removing the keydown listener if this event is received.
Objective-C
只需在“- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions”内添加代码行
Swift 2.x
添加单个 中的代码行
appDelegate.swift didFinishLaunchingWithOptions 方法Swift 3.x
Objective-C
Just add the line of code inside "- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions"
Swift 2.x
Add single line of code in appDelegate.swift didFinishLaunchingWithOptions method
Swift 3.x
也许在您的 DeviceMotionEvent 侦听器中尝试
event.preventDefault();
?环顾 Stack Overflow,我可以看到其他人已经成功地使用 CSS 禁用了默认“事件”(奇怪的是)。
防止默认按但不默认拖入iOS MobileSafari?
Perhaps try
event.preventDefault();
in your DeviceMotionEvent listener ?Looking around Stack Overflow, I can see other people have had success disabling default "events" using CSS (oddly).
Prevent default Press but not default Drag in iOS MobileSafari?
请参阅:如何停止 UITextField 响应摇动手势?
对于phonegap,请在AppDelegate.m 中的webViewFinishLoad 方法中设置此属性
See: How to stop the UITextField from responding to the shake gesture?
For phonegap, set this property within the webViewFinishLoad method in AppDelegate.m
第一个解决方案
最简单的方法是使用这个插件:
https: //github.com/nleclerc/cordova-plugin-ios-disableshaketoedit
第二种解决方案
如果您不想使用上述插件,那么您应该手动执行此操作打开 MainViewController.m 并将 webViewDidFinishLoad 更改为如下所示:
第三种解决方案
这是我设计的另一个解决方案,用于禁用适用于文本输入的“Shake To Undo”。请注意,不允许用户插入表情符号。
First solution
The easiest way is to use this plugin:
https://github.com/nleclerc/cordova-plugin-ios-disableshaketoedit
Second solution
If you don't want to use the above plugin then you should do it manually by opening the MainViewController.m and change webViewDidFinishLoad to look like this:
Third solution
it's another solution i designed to disable "Shake To Undo" which works on text inputs. note that users are not allowed to insert emojis.
我找到了一个一致有效的解决方法:您可以将文本输入托管在框架中。完成输入后,从页面中删除框架。这似乎可以解决问题。
这是概念证明:https://codepen.io/joshduck/full/RJMYRd/
I found a workaround that works consistently: you can host the text input in a frame. When you're done with the input, remove the frame from the page. This seems to do the trick.
Here's a proof of concept: https://codepen.io/joshduck/full/RJMYRd/
将输入放入 iframe 中,不需要输入时重新加载 iframe。
Put the input into an iframe, reload the iframe when you don't need the input.