この分野の超初心者です。
ボタンを3D Touchした時に別のアクションを起こさせるような効果を追加したいと思っています。
(pick and pop ではなく)
しかしどのようなコードを書けば良いのかわかりません。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
Home Screen Quick Actionsはどうでしょうか?
まず、Assets.xcassetsの中にアイコンにしたい画像を設定します。
次にinfo.plistに下記のようにタイトルや画像ファイル名を指定します。
※UIApplicationShortcutItemTypeは下記に記載します。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> ...アプリの設定情報などは省略 <key>UIApplicationShortcutItems</key> <array> <dict> <key>UIApplicationShortcutItemIconFile</key> <string>inu</string> <key>UIApplicationShortcutItemTitle</key> <string>dog</string> <key>UIApplicationShortcutItemSubtitle</key> <string>dogdog</string> <key>UIApplicationShortcutItemType</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER).openDog</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>version</key> <string>${APP_VERSION}</string> </dict> </dict> <dict> <key>UIApplicationShortcutItemIconFile</key> <string>same</string> <key>UIApplicationShortcutItemTitle</key> <string>shark</string> <key>UIApplicationShortcutItemType</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER).openShark</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>version</key> <string>${APP_VERSION}</string> </dict> </dict> <dict> <key>UIApplicationShortcutItemIconFile</key> <string>risu</string> <key>UIApplicationShortcutItemTitle</key> <string>squirrel</string> <key>UIApplicationShortcutItemType</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER).openSquirrel</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>version</key> <string>${APP_VERSION}</string> </dict> </dict> <dict> <key>UIApplicationShortcutItemIconFile</key> <string>hitode</string> <key>UIApplicationShortcutItemTitle</key> <string>starfish</string> <key>UIApplicationShortcutItemSubtitle</key> <string>star!!!!!!!!!!!!!!!!</string> <key>UIApplicationShortcutItemType</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER).openStarfish</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>version</key> <string>${APP_VERSION}</string> </dict> </dict> </array> </dict> </plist>
3D Touchを行なった際に下記のような形で表示されます。
次にAppDelegate.swift内で
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // アプリが起動していない場合に3D Touchでアプリを起動すると // まずperformActionForの前にこの処理が起きるため、同じ処理を2回しないようにreturn falseする if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem { handleShortcut(shortcutItem) return false } return true } } extension AppDelegate { // info.plistのUIApplicationShortcutItemTypeに設定する値 enum ShortcutIdentifier: String { case openDog case openShark case openSquirrel case openStarfish init?(identifier: String) { // openDog, openSharkなどの文字列を取得 guard let shortIdentifier = identifier.components(separatedBy: ".").last else { return nil } self.init(rawValue: shortIdentifier) } } // ここで3D Touchのアクションを受け取る func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { completionHandler(handleShortcut(shortcutItem)) } @discardableResult private func handleShortcut(_ shortcutItem: UIApplicationShortcutItem) -> Bool { let shortcutType = shortcutItem.type guard let itemType = ShortcutIdentifier(identifier: shortcutType) else { return false } return shouldPerformActionFor(itemType) } private func shouldPerformActionFor(_ itemType: ShortcutIdentifier) -> Bool { guard let vc = window?.rootViewController as? ViewController else { return false } var message: String = "" // 押されたアクションに応じて処理をする(今回はラベルの値を変更しているのみ) switch itemType { case .openDog: message = "inu!" case .openShark: message = "same!" case .openSquirrel: message = "risu!" case .openStarfish: message = "star!" } vc.setLabel(message: message) return true } }
画面を表示するViewControllerは、
import UIKit class ViewController: UIViewController { @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setLabel(message: String) { self.label.text = message } }
こちらのブログなど参考になるかもしれません。
的外れでしたらすいません。
投稿2018/02/26 23:28
総合スコア1277
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/27 02:07
2018/02/27 21:41 編集
2018/03/02 07:11