SpriteKit の SKScene から DUALSHOCK 4 のボタンを検知する方法です。
以下のコードを実行する前に iPhone と DUALSHOCK 4を Bluetooth でリンクする必要があります。
以下の参考にして接続してください。
iOS 13の新機能:iPhone/iPadとPS4コントローラの繋ぎ方・使い方を解説
https://mobilelaby.com/blog-entry-how-to-connect-ps4-controller.html
接続できたら、以下のコードを Xcode で実行してください。
実行中にボタンを押すと、デバッグコンソールに各ボタンが押された旨の表示が print によって出ます。
import SpriteKit import GameController class GameScene: SKScene { override func sceneDidLoad() { self.scaleMode = .resizeFill self.anchorPoint = CGPoint(x: 0.5, y: 0.5) setupNotificationCenter() } func setupNotificationCenter() { NotificationCenter.default.addObserver(self, selector: #selector(self.handleControllerDidConnectNotification(_:)), name: .GCControllerDidConnect, object: nil) } @objc func handleControllerDidConnectNotification(_ notification: NSNotification) { print("GCControllerDidConnect") guard let controller = notification.object as? GCController, let gamepad = controller.extendedGamepad else { return } print(String(describing: controller.vendorName)) handleGameController(gamepad: gamepad) } }
以下がボタンを検知する処理です
func handleGameController(gamepad: GCExtendedGamepad) { // △ ○ × □ var triangleButton: GCControllerButtonInput? var circleButton: GCControllerButtonInput? var crossButton:GCControllerButtonInput? var rectButton:GCControllerButtonInput? // ↑ ← ↓ → key var directionPad: GCControllerDirectionPad? // thumbstich var leftThumbstick: GCControllerDirectionPad? var rightThumbstick: GCControllerDirectionPad? var leftThumbstickButton: GCControllerButtonInput? var rightThumbstickButton: GCControllerButtonInput? // L1, L2, R1, R2 button var l1Button: GCControllerButtonInput? var l2Button: GCControllerButtonInput? var r1Button: GCControllerButtonInput? var r2Button: GCControllerButtonInput? // setting var optionsButton: GCControllerButtonInput? var shareButton: GCControllerButtonInput? // △ ○ × □ rectButton = gamepad.buttonX triangleButton = gamepad.buttonY circleButton = gamepad.buttonB crossButton = gamepad.buttonA // ↑ ← ↓ → key directionPad = gamepad.dpad // thumbstich leftThumbstick = gamepad.leftThumbstick rightThumbstick = gamepad.rightThumbstick leftThumbstickButton = gamepad.leftThumbstickButton rightThumbstickButton = gamepad.rightThumbstickButton // L1, L2, R1, R2 button l1Button = gamepad.leftShoulder l2Button = gamepad.leftTrigger r1Button = gamepad.rightShoulder r2Button = gamepad.rightTrigger // setting optionsButton = gamepad.buttonMenu shareButton = gamepad.buttonOptions // △ ○ × □ triangleButton?.pressedChangedHandler = { (input, value, isPressed) in guard isPressed else { return } print("press △") } circleButton?.pressedChangedHandler = { (input, value, isPressed) in guard isPressed else { return } print("press ○") } crossButton?.pressedChangedHandler = { (input, value, isPressed) in guard isPressed else { return } print("press ×") } rectButton?.pressedChangedHandler = { (input, value, isPressed) in guard isPressed else { return } print("press □") } // ↑ ← ↓ → key directionPad?.valueChangedHandler = {(_ dPad: GCControllerDirectionPad, _ x: Float, _ y: Float) -> Void in if x == 0 && y == 0 { return } print("x: \(x), y: \(y)") if x == 1.0 { print("→") } if x == -1.0 { print("←") } if y == 1.0 { print("↑") } if y == -1.0 { print("↓") } } // thumbstich leftThumbstick?.valueChangedHandler = {(_ dpad: GCControllerDirectionPad, _ xValue: Float, _ yValue: Float) -> Void in print("left stick X: \(xValue)") print("left stick Y: \(yValue)") } rightThumbstick?.valueChangedHandler = {(_ dpad: GCControllerDirectionPad, _ xValue: Float, _ yValue: Float) -> Void in print("right stick X: \(xValue)") print("right stick Y: \(yValue)") } leftThumbstickButton?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("left thumbstick pressed") } } rightThumbstickButton?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("right thumbstick presded") } } // L1, L2, R1, R2 button l1Button?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("L1") } } l2Button?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("L2") } } r1Button?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("R1") } } r2Button?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("R2") } } // setting optionsButton?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("OPTIONS tapped") } } shareButton?.valueChangedHandler = {(_ button: GCControllerButtonInput, _ value: Float, _ pressed: Bool) -> Void in if pressed { print("share tapped") } } }
SwiftUIを学ぶのにおすすめの本
SwiftUI 徹底入門
SwiftUIではじめるiPhoneアプリプログラミング入門
iOS/macOS UIフレームワーク SwiftUIプログラミング
コメント