Jul 27 2014
Swift でモーションセンサーの値を取るプログラムを書いてみた
はじめに
iOS の開発言語として新たに Swift が発表されました。Objective-C に比べると取っ付き易くモダンな言語とのこと。確かに、言語仕様としてはかなり自由度が高く(特に switch-case がすごい)、ポインタの概念もないので落ちにくいプログラムを書くことができそうです。
ただ、Cocoa フレームワークの仕組みはそのままなので、アプリの作り方が劇的に変わるかというとそうではありません。あくまでも Objective-C の代わりに Swift で書けるという言語的な違いだけです。
モーションデータ取るだけのアプリ
何か書いてみようと思って、ちょっと別件でモーションセンサーの値を使ってごにょごにょする試みをやろうとしていたので、単にモーションセンサーの値を取り出して表示するだけというマコトにシンプルなアプリを書いてみました。
鬼シンプル( ´∀`)
ソースコード
Xcode 6 の Beta 4 になってまたしても Swift の言語仕様が変わったのでそれに対応していますが、今後 Beta 5 になるとまたコンパイルエラーになるかもしれません。
AppDelegete なんかは Xcode が吐くデフォルトのままなので、ViewController.swift だけ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
// // ViewController.swift // Swift-Motion // // Created by gyoza manner on July 20, 2014 // Copyright (c) 2014 gyoza manner. All rights reserved. // import UIKit import CoreMotion class ViewController: UIViewController { // Connection with interface builder @IBOutlet var acc_x: UILabel! @IBOutlet var acc_y: UILabel! @IBOutlet var acc_z: UILabel! @IBOutlet var gyro_x: UILabel! @IBOutlet var gyro_y: UILabel! @IBOutlet var gyro_z: UILabel! @IBOutlet var attitude_roll: UILabel! @IBOutlet var attitude_pitch: UILabel! @IBOutlet var attitude_yaw: UILabel! @IBOutlet var attitude_x: UILabel! @IBOutlet var attitude_y: UILabel! @IBOutlet var attitude_z: UILabel! @IBOutlet var attitude_w: UILabel! // create instance of MotionManager let motionManager: CMMotionManager = CMMotionManager() override func viewDidLoad() { super.viewDidLoad() // Initialize MotionManager motionManager.deviceMotionUpdateInterval = 0.05 // 20Hz // Start motion data acquisition motionManager.startDeviceMotionUpdatesToQueue( NSOperationQueue.currentQueue(), withHandler:{ deviceManager, error in var accel: CMAcceleration = deviceManager.userAcceleration self.acc_x.text = String(format: "%.2f", accel.x) self.acc_y.text = String(format: "%.2f", accel.y) self.acc_z.text = String(format: "%.2f", accel.z) var gyro: CMRotationRate = deviceManager.rotationRate self.gyro_x.text = String(format: "%.2f", gyro.x) self.gyro_y.text = String(format: "%.2f", gyro.y) self.gyro_z.text = String(format: "%.2f", gyro.z) var attitude: CMAttitude = deviceManager.attitude self.attitude_roll.text = String(format: "%.2f", attitude.roll) self.attitude_pitch.text = String(format: "%.2f", attitude.pitch) self.attitude_yaw.text = String(format: "%.2f", attitude.yaw) var quaternion: CMQuaternion = attitude.quaternion self.attitude_x.text = String(format: "%.2f", quaternion.x) self.attitude_y.text = String(format: "%.2f", quaternion.y) self.attitude_z.text = String(format: "%.2f", quaternion.z) self.attitude_w.text = String(format: "%.2f", quaternion.w) }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
こんだけです。
なお、プロジェクト全体は GitHub にも置きました。現段階では GitHub に公開するほどの分量ではないですが、今後これにいろいろ足していくかもしれません。