|
| 1 | +/* |
| 2 | + Copyright (C) 2016 Apple Inc. All Rights Reserved. |
| 3 | + See LICENSE.txt for this sample’s licensing information |
| 4 | + |
| 5 | + Abstract: |
| 6 | + A view controller to display output from the motion sensor. |
| 7 | + */ |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import CoreMotion |
| 11 | +import simd |
| 12 | + |
| 13 | +class DeviceMotionViewController: UIViewController, MotionGraphContainer { |
| 14 | + |
| 15 | + // MARK: Properties |
| 16 | + |
| 17 | + @IBOutlet var graphSelector: UISegmentedControl! |
| 18 | + |
| 19 | + @IBOutlet var graphsContainer: UIView! |
| 20 | + |
| 21 | + private var selectedDeviceMotion: DeviceMotion { |
| 22 | + return DeviceMotion(rawValue: graphSelector.selectedSegmentIndex)! |
| 23 | + } |
| 24 | + |
| 25 | + private var graphViews: [GraphView] = [] |
| 26 | + |
| 27 | + // MARK: MotionGraphContainer properties |
| 28 | + |
| 29 | + var motionManager: CMMotionManager? |
| 30 | + |
| 31 | + @IBOutlet weak var updateIntervalLabel: UILabel! |
| 32 | + |
| 33 | + @IBOutlet weak var updateIntervalSlider: UISlider! |
| 34 | + |
| 35 | + let updateIntervalFormatter = MeasurementFormatter() |
| 36 | + |
| 37 | + @IBOutlet var valueLabels: [UILabel]! |
| 38 | + |
| 39 | + // MARK: UIViewController overrides |
| 40 | + |
| 41 | + override func viewDidLoad() { |
| 42 | + super.viewDidLoad() |
| 43 | + |
| 44 | + // Create graph views for each graph type. |
| 45 | + graphViews = DeviceMotion.allTypes.map { type in |
| 46 | + return GraphView(frame: graphsContainer.bounds) |
| 47 | + } |
| 48 | + |
| 49 | + // Add the graph views to the container view. |
| 50 | + for graphView in graphViews { |
| 51 | + graphsContainer.addSubview(graphView) |
| 52 | + graphView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + override func viewWillAppear(_ animated: Bool) { |
| 57 | + super.viewWillAppear(animated) |
| 58 | + |
| 59 | + startUpdates() |
| 60 | + } |
| 61 | + |
| 62 | + override func viewDidDisappear(_ animated: Bool) { |
| 63 | + super.viewDidDisappear(animated) |
| 64 | + |
| 65 | + stopUpdates() |
| 66 | + } |
| 67 | + |
| 68 | + // MARK: Interface Builder actions |
| 69 | + |
| 70 | + @IBAction func intervalSliderChanged(_ sender: UISlider) { |
| 71 | + startUpdates() |
| 72 | + } |
| 73 | + |
| 74 | + @IBAction func graphSelectorChanged(_ sender: UISegmentedControl) { |
| 75 | + showGraph(selectedDeviceMotion) |
| 76 | + } |
| 77 | + |
| 78 | + // MARK: MotionGraphContainer implementation |
| 79 | + |
| 80 | + func startUpdates() { |
| 81 | + guard let motionManager = motionManager, motionManager.isDeviceMotionAvailable else { return } |
| 82 | + |
| 83 | + showGraph(selectedDeviceMotion) |
| 84 | + updateIntervalLabel.text = formattedUpdateInterval |
| 85 | + |
| 86 | + motionManager.deviceMotionUpdateInterval = TimeInterval(updateIntervalSlider.value) |
| 87 | + motionManager.showsDeviceMovementDisplay = true |
| 88 | + |
| 89 | + motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: .main) { deviceMotion, error in |
| 90 | + guard let deviceMotion = deviceMotion else { return } |
| 91 | + |
| 92 | + let attitude = double3([deviceMotion.attitude.roll, deviceMotion.attitude.pitch, deviceMotion.attitude.yaw]) |
| 93 | + let rotationRate = double3([deviceMotion.rotationRate.x, deviceMotion.rotationRate.y, deviceMotion.rotationRate.z]) |
| 94 | + let gravity = double3([deviceMotion.gravity.x, deviceMotion.gravity.y, deviceMotion.gravity.z]) |
| 95 | + let userAcceleration = double3([deviceMotion.userAcceleration.x, deviceMotion.userAcceleration.y, deviceMotion.userAcceleration.z]) |
| 96 | + |
| 97 | + self.graphView(for: .attitude).add(attitude) |
| 98 | + self.graphView(for: .rotationRate).add(rotationRate) |
| 99 | + self.graphView(for: .gravity).add(gravity) |
| 100 | + self.graphView(for: .userAcceleration).add(userAcceleration) |
| 101 | + |
| 102 | + // Update the labels with data for the currently selected device motion. |
| 103 | + switch self.selectedDeviceMotion { |
| 104 | + case .attitude: |
| 105 | + self.setValueLabels(rollPitchYaw: attitude) |
| 106 | + |
| 107 | + case .rotationRate: |
| 108 | + self.setValueLabels(xyz: rotationRate) |
| 109 | + |
| 110 | + case .gravity: |
| 111 | + self.setValueLabels(xyz: gravity) |
| 112 | + |
| 113 | + case .userAcceleration: |
| 114 | + self.setValueLabels(xyz: userAcceleration) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + func stopUpdates() { |
| 120 | + guard let motionManager = motionManager, motionManager.isDeviceMotionActive else { return } |
| 121 | + |
| 122 | + motionManager.stopDeviceMotionUpdates() |
| 123 | + } |
| 124 | + |
| 125 | + // MARK: Convenience |
| 126 | + |
| 127 | + private func graphView(for motionType: DeviceMotion) -> GraphView { |
| 128 | + let index = motionType.rawValue |
| 129 | + return graphViews[index] |
| 130 | + } |
| 131 | + |
| 132 | + private func showGraph(_ motionType: DeviceMotion) { |
| 133 | + let selectedGraphIndex = motionType.rawValue |
| 134 | + |
| 135 | + for (index, graph) in graphViews.enumerated() { |
| 136 | + graph.isHidden = index != selectedGraphIndex |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +fileprivate enum DeviceMotion: Int { |
| 143 | + case attitude, rotationRate, gravity, userAcceleration |
| 144 | + |
| 145 | + static let allTypes: [DeviceMotion] = [.attitude, .rotationRate, .gravity, .userAcceleration] |
| 146 | +} |
0 commit comments