質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

321閲覧

Swift4でボタンに3D Touchの効果を検知させるようにはできますか?

punyaxa

総合スコア11

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

1クリップ

投稿2018/02/26 11:31

この分野の超初心者です。
ボタンを3D Touchした時に別のアクションを起こさせるような効果を追加したいと思っています。
(pick and pop ではなく)
しかしどのようなコードを書けば良いのかわかりません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答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

newmt

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

punyaxa

2018/02/27 02:07

丁寧なご回答をきていただきありがとうございます???? 今回私が作りたいと考えているのはアプリ内に配置したボタンを3Dタッチすることによって普通にタップする時とは別のアクションを起こせるようにするというものだったのですが、今回説明頂いた内容の機能も実装したいと思っていたので助かりました。 もし出来ればその機能についての解説や解説のページを紹介頂けないでしょうか?
newmt

2018/02/27 21:41 編集

検討違いな回答でごめんなさい。 このサイトなどいかがでしょうか? https://qiita.com/kusumotoa/items/f33c89f150cd0937d003#_reference-33fa87d5c8c7541ea351 また、通常のボタンでしたらtouchesMovedメソッドの中でボタンを押す強さがわかるので そこで判定するでいかがでしょうか? ※コメント内でのコードの書き方がわからず見づらくなってすいません。 class customButton : UIButton{ override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch in touches{ print("% Touch pressure: (touch.force/touch.maximumPossibleForce)") } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { print("Touches End") } } 参考サイト: https://qiita.com/noppefoxwolf/items/ecb1a0caac9741b01e93 また検討違いでしたらすいません。 追記: 3DTouchが使えるかどうかの判定が必要な場合は func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool { return traitCollection.forceTouchCapability == .available } if(is3dTouchAvailable(traitCollection: self.view!.traitCollection)) { //... } で出来ます。
punyaxa

2018/03/02 07:11

早速実装を試してみます!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問