3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS:加速度センサーに触れてみる

Posted at

最近、Androidで加速度センサーに触れる機会があった。

その時iOSでまだ一回もセンサー関連触れたことがないなと思ったので、触れてみることにした。

import UIkit
import CoreMotion

使うのは上記の二つ。

var _motionManager: CMMotionManager!

    var _x: Float = 0
    
    var _y: Float = 0

今回は回転はさせないのでZは省く。

次にセンサー情報を通知を開始させる

_motionManager = CMMotionManager()
_motionManager.deviceMotionUpdateInterval = 1.0 / 60.0 //60FPS                    
_motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: {(motion, error) in self.updateMotion(motion!)})

最後に更新する部分を作る

func updateMotion(motion: CMDeviceMotion) {
    //端末の加速度を取得する
    let gravity = motion.gravity
    //ローバスフィルターを当てる
   _x = (Float(gravity.x) * RowPathFilter) + (_x * (1.0 - RowPathFilter))
   _y = (Float(gravity.x) * RowPathFilter) + (_y * (1.0 - RowPathFilter))

}

アプリ開始時
IMG_0012.PNG
右に傾ける
IMG_0013.PNG
左に傾ける
IMG_0014.PNG

使用機器:IpadPro iOS9.3.1

3
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?