iOS设备的重力感应

重力感应是每台iOS设备都具备的功能,所以在应用用好重力感应会有意想不到的效果

1.添加CoreMotion框架

NewImage

2.在需要使用重力感应的类中添加头文件

#import <CoreMotion/CoreMotion.h>

3.对MotionManager进行实例化并初始化

    CMMotionManager * motionManager;

    motionManager = [[CMMotionManageralloc]init];

4.开启设备的重力感应模式

    [motionManagerstartDeviceMotionUpdates];

5.获取重力在各个方向的分量(最小为-1,最大为1)

    double gravityX = motionManager.deviceMotion.gravity.x;

    double gravityY = motionManager.deviceMotion.gravity.y;

    double gravityZ = motionManager.deviceMotion.gravity.z;

NewImage

原文地址:https://www.cnblogs.com/wisejoker/p/3553218.html