A lot of sensors are built into the phone, such as: light sensor, humidity sensor, temperature sensor, distance sensor, etc.
The development of sensor-related things must use the real machine
In the spiral and accelerometer all two ways push and pull, push way is always detected, pulling the way is required to obtain the detection value
/*
Steps to use the accelerometer push:
1. Create a campaign manager
_mgr = [[Cmmotionmanager alloc] init];
2. Determine if the mobile accelerometer is available
if (!self.mgr.isaccelerometeravailable) {
NSLog (@ "Accelerometer is not available, please replace the phone");
Return
}
3. Set the sampling interval
Self.mgr.accelerometerUpdateInterval = 1.0/30.0;
4. Turn on detection
[Self.mgr startaccelerometerupdatestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (CMAccelerometerData * _ Nullable Accelerometerdata, Nserror * _nullable error) {
cmacceleration acceleration = accelerometerdata.acceleration;
NSLog (@ "x:%f y:%f z:%f", acceleration.x,acceleration.y,acceleration.z);
}];
2. Use of Gyro Pull method
1. Create a campaign manager
_mgr = [[Cmmotionmanager alloc] init];
2. Determine if the phone gyroscope is available
if (!self.mgr.isgyroavailable) {
NSLog (@ "Gyroscope not available");
Return
}
3. Turn on detection
[Self.mgr startgyroupdates];
4. Get the desired value
Cmrotationrate rotationrate = self.mgr.gyroData.rotationRate;
NSLog (@ "x:%f y:%f z:%f", rotationrate.x,rotationrate.y,rotationrate.z);
The use of accelerometers and gyroscopes is basically consistent--------------
The motion Manager controller is going to have it, otherwise it could be a local variable that cannot be used.
*/
#import <CoreMotion/CoreMotion.h> Import and motion-related frameworks
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface Viewcontroller ()
@property (nonatomic,strong) Cmmotionmanager * MGR;
@end
@implementation Viewcontroller
Lazy Loading Create campaign manager
-(Cmmotionmanager *) Mgr
{
if (_mgr = = nil) {
_mgr = [[Cmmotionmanager alloc] init];
}
return _mgr;
}
-(void) Viewdidload {
[Super Viewdidload];
}
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event
{
NSLog (@ "%zd,%@", motion,event);
NSLog (@ "Start shaking");
}
-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event
{
NSLog (@ "%zd,%@", motion,event);
NSLog (@ "Shake Cancel");
}
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event
{
NSLog (@ "%zd,%@", motion,event);
NSLog (@ "shaking end");
}
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
Accelerometer Correlation
cmacceleration acceleration = self.mgr.accelerometerData.acceleration;
NSLog (@ "x:%f y:%f z:%f", acceleration.x,acceleration.y,acceleration.z);
Accelerometer Correlation
Gyroscope related
Cmrotationrate rotationrate = self.mgr.gyroData.rotationRate;
NSLog (@ "x:%f y:%f z:%f", rotationrate.x,rotationrate.y,rotationrate.z);
}
-(void) Pullgyro
{
if (!self.mgr.isgyroavailable) {
NSLog (@ "Gyroscope not available");
Return
}
[Self.mgr startgyroupdates];
}
-(void) Pushgyro
{
Determine if the phone's gyroscope is available
if (!self.mgr.gyroavailable) {
NSLog (@ "Gyro is not available, please replace the phone");
Return
}
Set sampling interval
Self.mgr.gyroUpdateInterval = 1.0/20.0;
Start detection of gyroscope changes
[Self.mgr startgyroupdatestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (cmgyrodata * _Nullable GyroData, NSError * _nullable error) {
Cmrotationrate rotationrate = self.mgr.gyroData.rotationRate;
NSLog (@ "x:%f y:%f z:%f", rotationrate.x,rotationrate.y,rotationrate.z);
}];
}
-(void) Pullaccelerometer
{
if (!self.mgr.isaccelerometeravailable) {
NSLog (@ "Accelerometer is not available, please replace the phone");
Return
}
[Self.mgr startaccelerometerupdates];
}
-(void) Pushaccelerometer
{
Cmmotionmanager * mgr = [[Cmmotionmanager alloc] init];
Determine if the accelerometer is available
if (!self.mgr.isaccelerometeravailable) {
NSLog (@ "Accelerometer is not available, please replace the phone");
Return
}
Set sampling interval
Self.mgr.accelerometerUpdateInterval = 1.0/30.0;
[Self.mgr startaccelerometerupdatestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (CMAccelerometerData * _ Nullable Accelerometerdata, Nserror * _nullable error) {
cmacceleration acceleration = accelerometerdata.acceleration;
NSLog (@ "x:%f y:%f z:%f", acceleration.x,acceleration.y,acceleration.z);
}];
}
-(void) proximitymonitoring
{
Open distance Sensor
[Uidevice currentdevice].proximitymonitoringenabled = YES;
Change of listening distance
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (proximitystatedidchange:) Name: Uideviceproximitystatedidchangenotification Object:nil];
}
-(void) Proximitystatedidchange: (nsnotification*) Noti
{
if ([uidevice currentdevice].proximitystate = = YES) {
NSLog (@ "with items near");
}else{
NSLog (@ "Item left");
}
}
-(void) dealloc
{
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}
iOS Development sensor-related