A summary of how iphone sensors are invoked in IOS app _ios

Source: Internet
Author: User
Tags notification center

Use of the Coremotion framework

Coremotion Framework is very powerful, it not only the acceleration sensor and spiral sensor for the unified configuration and management, but also for us to encapsulate a number of algorithms, we can directly obtain the device's motion status information.

1, Coremotion is responsible for processing the data

Coremotion is responsible for processing four kinds of data, one is the acceleration data, one is the spiral data, one is the magnetic induction data, there is also a first three kinds of data through the complex operation of the equipment movement data. A few of the main classes are as follows:

Cmaccelerommterdata: Acceleration data for equipment

typedef struct {
    double x;
    double y;
    double z;
} CMAcceleration;
@interface CMAccelerometerData : CMLogItem
{
@private
    id _internal;
}
//加速度的数据对象
@property(readonly, nonatomic) CMAcceleration acceleration;

@end
CMGyroData:设备的螺旋仪数据

typedef struct {
    double x;
    double y;
    double z; 
} CMRotationRate;
@interface CMGyroData : CMLogItem
{
@private
    id _internal;
}
//螺旋仪数据对象
@property(readonly, nonatomic) CMRotationRate rotationRate;

@end
CMMagnetometerData:磁感应信息

typedef struct {
    double x;
    double y;
    double z;
} CMMagneticField;

@interface CMMagnetometerData : CMLogItem
{
@private
    id _internal;
}

//磁力对象
@property(readonly, nonatomic) CMMagneticField magneticField;

@end


CMDeviceMotion:设备的运动状态数据

@interface cmdevicemotion:cmlogitem
{
@private
    ID _internal;
}
//Device state object
@property (readonly, nonatomic) cmattitude *attitude;
//device angular speed
@property (readonly, nonatomic ) Cmrotationrate rotationrate;
//device's gravitational acceleration
@property (readonly, nonatomic) cmacceleration gravity;
//user marries the acceleration device of the device with a total acceleration of gravitational acceleration called user-given accelerations
@property (readonly, nonatomic) cmacceleration useracceleration;
//device magnetic field vector Object
@property (readonly, nonatomic) Cmcalibratedmagneticfield Magneticfield ns_available (na,5_0);
This is more complicated than the previous two classes, and the attitude object encapsulates the state properties of many devices:

@interface cmattitude:nsobject <nscopying, Nssecurecoding>
{
@private
    ID _internal;
}
//device Euler angle Roll
@property (readonly, nonatomic) double roll;
//device's Euler angle pitch
@property (ReadOnly, nonatomic) Double pitch;
//device Euler angle Yaw
@property (readonly, nonatomic) double yaw;
//device state rotation matrix
@property (readonly, nonatomic) Cmrotationmatrix Rotationmatrix;
//Device status four $
@property (readonly, nonatomic) cmquaternion quaternion;
@end
2, There are two ways to use the Coremotion
coremotion, one of which is to request data from the manager, and a callback for the manager to pass the data back to us. These two methods are referred to as pull and push methods, respectively.
Pull Way:

-(void) viewdidload {
    [super Viewdidload];
   //Do any additional SE Tup after loading the view, typically from a nib.
   //Create Admin Objects
    manager= [[Cmmotionmanager alloc]init];
    //Open speed update
    [manager startaccelerometerupdates];
   //Open Spiral Update
    [manager Startgyroupdates];
   //Open status update
    [manager startmagnetometerupdates];
   // Create timer
    Nstimer * time = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector ( Updata) Userinfo:nil Repeats:yes];
    time.firedate = [NSDate distantpast];
}

-(void) updata{
//Get Data
    NSLog (@ "%f,%f,%f\n%f,%f,%f", MANAGER.ACCELEROMETERDATA.ACCELERATION.X,MANAGER.ACCELEROMETERDATA.ACCELERATION.Y, Manager.accelerometerdata.acceleration.z,manager.gyrodata.rotationrate.x,manager.gyrodata.rotationrate.y, MANAGER.GYRODATA.ROTATIONRATE.Z);
  
   
}
push mode:

   //创建管理对象
    manager= [[CMMotionManager alloc]init];
    //在当前线程中回调
    [manager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
         NSLog(@"%f,%f,%f\n%f,%f,%f",manager.accelerometerData.acceleration.x,manager.accelerometerData.acceleration.y,manager.accelerometerData.acceleration.z,manager.gyroData.rotationRate.x,manager.gyroData.rotationRate.y,manager.gyroData.rotationRate.z);
    }];
3, Coremotion more properties and methods
@interface Cmmotionmanager:nsobject
{
@private
ID _internal;
}
Set up the acceleration sensor to update the frame rate
@property (Assign, nonatomic) Nstimeinterval accelerometerupdateinterval __tvos_prohibited;
is the acceleration sensor available
@property (ReadOnly, nonatomic, getter=isaccelerometeravailable) BOOL accelerometeravailable __tvos_prohibited;
is the acceleration sensor active
@property (ReadOnly, nonatomic, getter=isaccelerometeractive) BOOL accelerometeractive __tvos_prohibited;
Acceleration Sensor Data Object
@property (readonly, nullable) Cmaccelerometerdata *accelerometerdata __tvos_prohibited;
Pull to start updating acceleration data
-(void) startaccelerometerupdates __tvos_prohibited;
Push method to update acceleration data
-(void) Startaccelerometerupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (Cmaccelerometerhandler) Handler __ tvos_prohibited;
Stop updating acceleration data
-(void) stopaccelerometerupdates __tvos_prohibited;
Sensor Refresh frame rate of spiral instrument
@property (Assign, nonatomic) Nstimeinterval gyroupdateinterval __tvos_prohibited;
Whether the spiral instrument is available
@property (ReadOnly, nonatomic, getter=isgyroavailable) BOOL gyroavailable __tvos_prohibited;
Whether the spiral device is activated
@property (ReadOnly, nonatomic, getter=isgyroactive) BOOL gyroactive __tvos_prohibited;
Data of the spiral instrument
@property (readonly, nullable) Cmgyrodata *gyrodata __tvos_prohibited;
Pull way to start updating the spiral instrument
-(void) startgyroupdates __tvos_prohibited;
Push mode to start updating spiral instrument
-(void) Startgyroupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (cmgyrohandler) handler __tvos_prohibited;
Stop updating the spiral instrument
-(void) stopgyroupdates __tvos_prohibited;
Update frame rate with magnetic sensor
@property (Assign, nonatomic) nstimeinterval magnetometerupdateinterval ns_available (na,5_0) __tvos_prohibited;
is the device magnetic sensor available
@property (ReadOnly, nonatomic, getter=ismagnetometeravailable) BOOL magnetometeravailable ns_available (na,5_0) __ tvos_prohibited;
is the device magnetic sensor active
@property (ReadOnly, nonatomic, getter=ismagnetometeractive) BOOL magnetometeractive ns_available (na,5_0) __tvos_ prohibited;
Equipment Magnetic State data
@property (readonly, nullable) Cmmagnetometerdata *magnetometerdata ns_available (na,5_0) __tvos_prohibited;
Pull mode Update equipment Magnetic status
-(void) startmagnetometerupdates ns_available (na,5_0) __tvos_prohibited;
Push mode update device magnetic status
-(void) Startmagnetometerupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (cmmagnetometerhandler) Handler NS_ AVAILABLE (NA,5_0) __tvos_prohibited;
Stop Updating Device status
-(void) stopmagnetometerupdates ns_available (na,5_0) __tvos_prohibited;
Device status update frame rate
@property (Assign, nonatomic) Nstimeinterval devicemotionupdateinterval __tvos_prohibited;
Reference enumeration
+ (Cmattitudereferenceframe) availableattitudereferenceframes ns_available (na,5_0) __tvos_prohibited;
@property (readonly, nonatomic) cmattitudereferenceframe attitudereferenceframe ns_available (na,5_0) __tvos_ prohibited;
Device motion information is available
@property (ReadOnly, nonatomic, getter=isdevicemotionavailable) BOOL devicemotionavailable __tvos_prohibited;
Whether the device motion information is active
@property (ReadOnly, nonatomic, getter=isdevicemotionactive) BOOL devicemotionactive __tvos_prohibited;
Device motion Information Object
@property (readonly, nullable) cmdevicemotion *devicemotion __tvos_prohibited;
Pull a way to start refreshing motion information
-(void) startdevicemotionupdates __tvos_prohibited;
Push mode to start refreshing motion information
-(void) Startdevicemotionupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (Cmdevicemotionhandler) Handler __ tvos_prohibited;
Use a reference system
-(void) Startdevicemotionupdatesusingreferenceframe: (cmattitudereferenceframe) referenceframe NS_AVAILABLE (NA,5_0 ) __tvos_prohibited;
Push mode to start refreshing device motion information
-(void) Startdevicemotionupdatesusingreferenceframe: (cmattitudereferenceframe) referenceframe ToQueue: ( Nsoperationqueue *) Queue Withhandler: (Cmdevicemotionhandler) handler ns_available (NA,5_0) __tvos_prohibited;
Stop refreshing device motion information
-(void) stopdevicemotionupdates __tvos_prohibited;

Application of distance sensor
The iphone is built with a proximity sensor, located near the handset, and when we are on the phone, the phone's screen is automatically extinguished, which is controlled by the distance sensor.
When we develop the app, we can also call some of the interface methods from the sensor, if needed. The distance sensor interface is very simple, mainly through the notification center to the change of distance to notify.
First, we need to open the distance sensor application:

[UIDevice currentDevice].proximityMonitoringEnabled=YES;
Notice of monitoring distance change:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice) name:UIDeviceProximityStateDidChangeNotification object:nil];
In the callback method, we can listen to the distance state by following this property:

-(void)notice{
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"近距离");
    }else{
        NSLog(@"远距离");
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.