LoginSignup
65
63

More than 5 years have passed since last update.

iOS 9から追加された3D Touch shortcutsを実装してみる

Last updated at Posted at 2015-09-17

3D Touch shortcutsとは

ホーム画面のアイコンをForce Touch(強めに押す)ことで、下図のようなショートカットメニューを表示することができます。Home screen dynamic quick action とも呼ばれるそうです。

image
Appleのサイトより引用)

各項目には、それぞれタップされた際のアクションを割り当てることが可能です。

実装方法

3D Touch shortcuts の実装方法には、予め項目を設定しておく Static Quick Actions と、コード上で動的に割り当てる Dynamic Quick Actions の二通りの方法があります。

Static Quick Actions

予め Info.plist ファイルにメニューの項目を定義しておく方法です。

    <key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeSearch</string>
            <key>UIApplicationShortcutItemSubtitle</key>
            <string>shortcutSubtitle1</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>shortcutTitle1</string>
            <key>UIApplicationShortcutItemType</key>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER).First</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>firstShorcutKey1</key>
                <string>firstShortcutKeyValue1</string>
            </dict>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeShare</string>
            <key>UIApplicationShortcutItemSubtitle</key>
            <string>shortcutSubtitle2</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>shortcutTitle2</string>
            <key>UIApplicationShortcutItemType</key>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER).Second</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>secondShortcutKey1</key>
                <string>secondShortcutValue1</string>
            </dict>
        </dict>
    </array>

各メニューに表示するタイトル、アイコンや、タップされた際にアプリ側に送信するUserInfo(Dictionary)などを設定します。

各項目の意味はこちらにまとめられています。

Dynamic Quick Actions

コード上で項目を追加する方法です。

// Construct the items.
let shortcut1 = UIMutableApplicationShortcutItem(type: ShortcutIdentifier.Third.type, localizedTitle: "Play", localizedSubtitle: "Will Play an item", icon: UIApplicationShortcutIcon(type: .Play), userInfo: [
        AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Play.rawValue
    ]
)

let shortcut2 = UIMutableApplicationShortcutItem(type: ShortcutIdentifier.Fourth.type, localizedTitle: "Pause", localizedSubtitle: "Will Pause an item", icon: UIApplicationShortcutIcon(type: .Pause), userInfo: [
        AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Pause.rawValue
    ]
)

// Update the application providing the initial 'dynamic' shortcut items.
application.shortcutItems = [shortcut1, shortcut2]

メニュー項目 UIMutableApplicationShortcutItem の配列を作り、UIApplication オブジェクトの shortcutItems プロパティにセットします。

ユーザにメニューをカスタマイズさせたい場合は、こちらの Dynamic Quick Actions を使ったほうが良さそうですね。

起動時に呼ばれるデリゲート

ホーム画面からショートカットメニューが選択され、アプリが起動すると application:performActionForShortcutItem:completionHandler: メソッドが呼ばれます。この中に必要な処理を実装します。

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
    // Do something
    let succeeded = ...

    completionHandler(succeeded)
}

処理が完了したら、結果(Bool)を completionHandler に伝えてあげます。

サンプルコード

こちらに置いておきました。

screenshot.jpg

リンク

65
63
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65
63