在 iPhone6S 或 iPhone6S Plus 模拟器上模拟强制触摸/3D 触摸

我试图在 iPhone 6S 或 iPhone 6S Plus 模拟器上使用 Xcode 7 GM 模拟力触摸。特别是,我试图模拟的力量触摸图标的测试应用程序,这是实现应用程序快捷方式。

让我知道如果有一个解决方案来模拟这一点,我尝试了长按手表模拟器用来模拟力触摸,不工作,因为 iPhone 捕捉它作为一个长按手势。

更新: 我从 Apple 下载了 ViewControllerPreview 示例代码。运行他们的示例应用程序,我发现这个模拟器虽然是 iPhone6s,但不支持3D/Force 触摸功能。我仍然不确定它是否会支持这一功能,如果我要使用一个力触摸触控板的 Macbook。

enter image description here

编辑: 是否有人可以用一台配有强力触摸触摸板的苹果笔记本电脑,尝试使用触摸板上的强力触摸来模拟 iPhone 6s 或6s Plus 模拟器上的3D 触摸?我的理论是,你也许可以使用新 MacBook 上的强制触摸触摸板,在新的 iPhone 模拟器上模拟3D 触摸。如果你尝试的话,添加一个注释来记录结果,这样我就可以更新文章了。

编辑2: 似乎新型 Macbook 专业版的 Force Touch 触摸板没有帮助。

58981 次浏览

Force pressure in iOS (eg: Peek & Pop) is not exposed through Xcode 7.0 nor Xcode 7.1 Beta's Simulator. The pressure touch options are only relevant for watchOS devices in those versions of Simulator. If you have the Xcode 7.1 beta, you'll notice that the menu option is actually disabled.

With Xcode 7.3 and the iOS 9.3 and later simulator runtimes, you can test peek/pop functionality using a force touch trackpad.

It says here! at the bottom of the page....

Development Environment

Xcode 7 supports 3D Touch development. All the debugging features of Xcode are available for implementing the new features.

Keep the following in mind:

With Xcode 7.0 you must develop on a device that supports 3D Touch. Simulator in Xcode 7.0 does not support 3D Touch.

With Xcode 7.0 you must implement your peek and pop view controllers in code. Interface Builder in Xcode 7.0 does not provide graphical support for configuring view controllers or transitions for 3D Touch.

Be sure to test your app with 3D Touch both enabled and disabled, ensuring that all features are available to all users. On a 3D Touch device, you can disable 3D Touch in Settings > General > Accessibility > 3D Touch

So unfortunately it looks like we need to get our hands on the actual devices to do it. :-(

EDIT.....

As of Xcode 7.3 there is a way to do this if your MacBook's trackpad supports force touch. See answer by @iPrabu.

For the "Home Screen Quick Actions" touch I made the following:

  • created an object of UIMutableApplicationShortcutItem

  • put it into launchOptions to UIApplicationLaunchOptionsShortcutItemKey

  • and thats it

It looks like user did the 3d touch on the app icon.

    let shortcut = UIMutableApplicationShortcutItem(type: ShortcutIdentifier.Third.type,
localizedTitle: "Title",
localizedSubtitle: "Subtitle",
icon: UIApplicationShortcutIcon(type: .Play), userInfo: [
AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Play.rawValue
]
)


let launchOption = [
"UIApplicationLaunchOptionsShortcutItemKey" : shortcut
]


launchOptions = launchOption

(application:didFinishLaunchingWithOptions:)

For those that are developing Quick Actions (App Shortcuts) you can use this script that enables you to simulate a deep press on an app on the simulator.

https://github.com/DeskConnect/SBShortcutMenuSimulator

Here's a screenshot: enter image description here

For those that are looking at how to add the actions on the first place, there are two options, Static and Dynamic. You can set only 4 options, it will first show all Static ones, and if there's space, show Dynamic ones.

Here's how to add Static shortcuts. It's pretty easy, just add some keys on the main Info.plist

In this example i will be adding two static options one with Default Search icon, and the other with custom icon from asset catalog.

<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>Add Event (with custom icon)</string>
<key>UIApplicationShortcutItemType</key>
<string>com.reverse.appname.addevent</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>custom-icon-from-asset-catalog</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Here you can add some text below the option</string>
</dict>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>Search Event (with default icon)</string>
<key>UIApplicationShortcutItemType</key>
<string>com.reverse.appname.searchevents</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Here you can add some text below the option</string>
</dict>
</array>

There's a bit of code here that allows you to test 3D Touch peek & pop in the simulator. Bear in mind this is code for iOS 9.0, and some of the internal/private methods have changed in 9.1 so you'll need to make a few minor changes (Hint: the word 'Position' changed to 'Location' in one of the method names).

https://gist.github.com/nickfrey/07e2c6d8d2e5444fb91d

Both this and https://github.com/DeskConnect/SBShortcutMenuSimulator mentioned in other answers allow you to test for 3D Touch without an actual device fairly competently.

Officially Xcode 7.1 supports 3D touch but not on simulator. You need to have actual device in order to test. Check last paragraph of documentation from Apple.

Apple Document

3D touch not supported on simulator

However, you can use third-party tools to test it on simulator such as one highlighted by Nicolas S above.

You can enable 3D Touch in simulator. Thanks to this library.

  • After adding it Using a combination of the command, control, and shift keys 3D Touch can be enabled.

  • Don't forget to add library in Debug environment only. Exclude it from Release environment.

The newer macbooks are having force-touch enabled touch pad. I think these options will only work in that.

With Xcode 7.3 you can test the 3D touch feature in simulator but with only one constraint. (Xcode7.3 release notes reference)

The machine(macbook/mac with trackpad) your are developing should have 3D touch capability.

You can find it under iOS simulator Hardware menu.

Comments from Pavel Alexeev: Be sure to enable “Force Click and haptic feedback” in System Preferences → Trackpad

enter image description here

It worked for me with XCode 9 & iphone 8 simulator. In your mac go to system preference -> Trackpad -> Enable Force click and haptic feedback.

enter image description here

Then press firmly on any part of iphone simulator to perform force touch action.