teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

プログラムを全部載せました

2017/11/24 08:48

投稿

SKMT
SKMT

スコア57

title CHANGED
File without changes
body CHANGED
@@ -7,16 +7,84 @@
7
7
 
8
8
  ###該当のソースコード
9
9
  ```swift4
10
+ import UIKit
11
+ import CoreMotion
12
+
13
+ class ViewController: UIViewController {
14
+
15
+ let motionManager = CMMotionManager()
16
+ var dataX:[Double] = []
17
+ var dataY:[Double] = []
18
+ var dataZ:[Double] = []
19
+ var btn = false
20
+
21
+ @IBOutlet weak var accelX: UILabel!
22
+ @IBOutlet weak var accelY: UILabel!
23
+ @IBOutlet weak var accelZ: UILabel!
24
+
25
+ override func viewDidLoad() {
26
+ super.viewDidLoad()
27
+ if motionManager.isAccelerometerAvailable {
28
+ motionManager.accelerometerUpdateInterval = 0.5
29
+
30
+ motionManager.startAccelerometerUpdates(
31
+ to: OperationQueue.current!,
32
+ withHandler: {(accelData: CMAccelerometerData?, errorOC: Error?) in
33
+ self.outputAccelData(acceleration: accelData!.acceleration)
34
+ })
35
+
36
+ }
37
+
38
+ }
39
+
40
+ func outputAccelData(acceleration: CMAcceleration){
41
+ accelX.text = String(format: "x = %.2f", acceleration.x)
42
+ accelY.text = String(format: "y = %.2f", acceleration.y)
43
+ accelZ.text = String(format: "z = %.2f", acceleration.z)
44
+
45
+ if (btn) {
46
+ dataX.append(acceleration.x)
47
+ dataY.append(acceleration.y)
48
+ dataZ.append(acceleration.z)
49
+ }
50
+
51
+ if ((acceleration.x >= 1.0)||(acceleration.x <= -1.0)) {
52
+ accelX.textColor = UIColor.red }
53
+ else if((acceleration.x <= 1.0)||(acceleration.x >= -1.0)) {
54
+ accelX.textColor = UIColor.black }
55
+ if((acceleration.y >= 1.0)||(acceleration.y <= -1.0)) {
56
+ accelY.textColor = UIColor.red }
57
+ else if((acceleration.y <= 1.0)||(acceleration.y >= -1.0)) {
58
+ accelY.textColor = UIColor.black }
59
+ if((acceleration.z >= 1.0)||(acceleration.z <= -1.0)) {
60
+ accelZ.textColor = UIColor.red }
61
+ else if((acceleration.z <= 1.0)||(acceleration.z >= -1.0)) {
62
+ accelZ.textColor = UIColor.black }
63
+ }
64
+
65
+ func stopAccelerometer(){
66
+ if (motionManager.isAccelerometerActive) {
67
+ motionManager.stopAccelerometerUpdates()
68
+ }
69
+ }
70
+
10
- @IBAction func TapBtn(_ sender: Any) {
71
+ @IBAction func TapBtn(_ sender: Any) {
11
72
  if (btn == true) {
12
73
  btn = false
13
- TapBtn.setTitle("計測中")
14
74
  }
15
75
  else {
16
76
  btn = true
17
77
  }
18
78
 
19
79
  }
80
+
81
+
82
+ override func didReceiveMemoryWarning() {
83
+ super.didReceiveMemoryWarning()
84
+ }
85
+
86
+
87
+ }
20
88
  ```
21
89
 
22
90
  ###試したこと