LoginSignup
0
1

More than 1 year has passed since last update.

[SwiftUI / CoreMotion]iPhone で測定した気圧を画面に表示する

Last updated at Posted at 2022-11-06

記事の内容

iPhone で測定した気圧を画面に表示する

開発環境

項目 内容 備考
PC MacBook Air M1 (2020) メモリ:16GB ストレージ:1TB
macOS Ventura(ver 13.0)
iPhone iPhone SE(2nd) iOS 16.1
IDE Xcode ver 14.1

注意事項

  • このアプリを起動するためには PC と iPhone を接続する必要があります。
  • 下記で紹介するコードにはエラー処理を実装していません。必要に応じて実装してください。

ポイント

クロージャを利用して気圧のデータを ContentView に渡す

測定した値を渡す処理
func startUpdate(completion: @escaping (Double) -> Void){
    //
    completion(/* 測定した値(Double)を引数に */)
}
画面に表示する処理
altimator.startUpdate { value in
    self.pressureValue = value // 画面が持っているプロパティに代入
}

コード

Altimator.swift
import Foundation
import CoreMotion

final class Altimator {
    var altimeter: CMAltimeter?
    
    init() {
        altimeter = CMAltimeter()
    }
    
    func startUpdate(completion: @escaping (Double) -> Void) {
        guard let altimeter = altimeter else {
            return
        }
        
        if CMAltimeter.isRelativeAltitudeAvailable() {
            altimeter.startRelativeAltitudeUpdates(to: OperationQueue.main) { data, error in
                if error == nil {
                    guard let pressure = data?.pressure.doubleValue else {
                        return
                    }
                    print("気圧", pressure * 10)
                    completion(pressure * 10)
                } else {
                    // エラー対応
                }
            }
        }
    }
    
    func stopUpdate() {
        altimeter?.stopRelativeAltitudeUpdates()
    }
}
ContentView.swift
import SwiftUI
import CoreMotion

struct ContentView: View {
    private let altimator = Altimator()
    @State var pressureValue = 0.0
    
    var body: some View {
        VStack {
            Text(String(format: "%.2f",pressureValue))
                .font(.largeTitle)
                .fontWeight(.semibold)
            Text("hPa")
                .font(.title)
                .fontWeight(.semibold)
            startButton
            stopButton
        }
    }
    
    private var startButton: some View {
        Button {
            altimator.startUpdate { value in
                self.pressureValue = value
            }
        } label: {
            Text("測定開始")
        }
        .buttonStyle(.borderedProminent)
    }
    
    private var stopButton: some View {
        Button {
            altimator.stopUpdate()
        } label: {
            Text("測定終了")
        }
        .buttonStyle(.bordered)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

実行イメージ (動画)

  • PC と iPhone を接続し、実装したアプリを実行
  • アプリを実行し 「測定開始」 ボタンをクリックすると、iPhone が測定した気圧を表示

GitHub

謝辞

CoreMotion を利用して気圧を測定するコードは下記記事で紹介されているコードを流用しました。
(わかりやすいのでオススメです)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1