加速度計の値の取得はviewDidLoadの中で処理し、Label.textに設定済だとして。
CSVへの保存タイミングをどうするかがわからないので、ボタンを押した時にラベルの値を保存してみたところ、動きました。
とりあえず動きましたレベルですが、何かの足しにでもなればと思います。
やりたいことは定期的にデータを取得して配列に追加していくようなイメージでしょうか?
swift
1 // ラベルのoutlet接続
2 @IBOutlet weak var xValueLabel : UILabel !
3 @IBOutlet weak var yValueLabel : UILabel !
4 @IBOutlet weak var zValueLabel : UILabel !
5
6
7
8 // ボタンタップ時にラベルの値を取得してCSVに保存する。
9 @IBAction func saveCSV ( _ sender : UIButton ) {
10 let fm = FileManager . default
11 //documents
12 let documentsPath = NSSearchPathForDirectoriesInDomains ( . documentDirectory , . userDomainMask , true ) . first !
13 let filePath = documentsPath + "/getdata.csv"
14 if ! fm . fileExists ( atPath : filePath ) {
15 fm . createFile ( atPath : filePath , contents : nil , attributes : [ : ] ) }
16
17 //let X = acceleration.x
18 //let Y = acceleration.y
19 //let Z = acceleration.z
20 //let getdata = "(X),(Y),(Z)\n"
21
22 let X = xValueLabel . text ?? ""
23 let Y = yValueLabel . text ?? ""
24 let Z = zValueLabel . text ?? ""
25 let getdata = X + Y + Z
26
27 print ( "getdata:" , getdata )
28
29 do {
30 try getdata . write ( toFile : filePath , atomically : true , encoding : String . Encoding . utf8 )
31 print ( "filePath:" , filePath )
32 print ( "Success to Wite the File" )
33 } catch let error as NSError {
34 print ( "Failure to Write File\n(error)" )
35 }
36 }
37
あまりにもアレだと思うので、2秒ごとに配列に加速度データ数値を追加して、ボタンを押したらCSVに変換してoutputするやつを置いときます。参考になる部分でもありましたら、ご自身のコードの中に取り入れてみでください。
swift
1 import UIKit
2 import CoreMotion
3
4 class ViewController : UIViewController {
5
6 // ラベルのoutlet接続
7 @IBOutlet weak var xAccelValuLabel : UILabel !
8 @IBOutlet weak var yAccelValuLabel : UILabel !
9 @IBOutlet weak var zAccelValuLabel : UILabel !
10
11 // 加速度データを記憶する配列
12 var accelValueArray = [ String ] ( )
13
14 // creMotionManagerを生成する
15 let cmManager = CMMotionManager ( )
16
17
18 override func viewDidLoad ( ) {
19 super . viewDidLoad ( )
20 // Do any additional setup after loading the view, typically from a nib.
21
22 // queueを実行する間隔(2.0秒 ← 変更可能)
23 cmManager . deviceMotionUpdateInterval = 2.0
24 // queueで実行するcloure
25 let handler : CMDeviceMotionHandler = { ( motionData : CMDeviceMotion ? , error : Error ? ) -> Void in
26 self . motionAnimation ( motionData , error : error as Error ? )
27 }
28 // 更新で実行するqueueを登録してモーションセンサーをスタートする
29 cmManager . startDeviceMotionUpdates ( to : OperationQueue . main , withHandler : handler )
30 }
31
32 // デバイスモーションセンサーで定期的に実行するメソッド
33 func motionAnimation ( _ motionData : CMDeviceMotion ? , error : Error ? ) {
34 if let motion = motionData {
35 // x軸方向加速度
36 let accelX = motion . userAcceleration . x
37 xAccelValuLabel . text = String ( accelX )
38 // y軸方向加速度
39 let accelY = motion . userAcceleration . y
40 yAccelValuLabel . text = String ( accelY )
41 // z軸方向加速度
42 let accelZ = motion . userAcceleration . z
43 zAccelValuLabel . text = String ( accelZ )
44
45 let geteData = "(accelX),(accelY),(accelZ)"
46 // 2秒毎に数値を配列に追加する
47 accelValueArray . append ( geteData )
48 print ( "accelValueArray:" , accelValueArray )
49 }
50 }
51
52 // accelValueArrayをCSVに変換して保存する
53 @IBAction func saveCSV ( _ sender : UIButton ) {
54 let fm = FileManager . default
55 //documents
56 let documentsPath = NSSearchPathForDirectoriesInDomains ( . documentDirectory , . userDomainMask , true ) . first !
57 let filePath = documentsPath + "/getdata.csv"
58 if ! fm . fileExists ( atPath : filePath ) {
59 fm . createFile ( atPath : filePath , contents : nil , attributes : [ : ] ) }
60
61 // 配列をCSVに変換する
62 let getData = accelValueArray . map { "\"" + $0 + "\"" } . joined ( separator : "," )
63
64 do {
65 try getData . write ( toFile : filePath , atomically : true , encoding : String . Encoding . utf8 )
66 print ( "Success to Wite the File" )
67 print ( "getData:" , getData )
68 print ( "filePath:" , filePath )
69 } catch let error as NSError {
70 print ( "Failure to Write File\n(error)" )
71 }
72 }
73
74 override func didReceiveMemoryWarning ( ) {
75 super . didReceiveMemoryWarning ( )
76 // Dispose of any resources that can be recreated.
77 }
78 }
79