iOS new accelerometer event (gyroscope and accelerometer)

Source: Internet
Author: User
Tags uikit

iOS New Accelerometer Event

"iOS New Accelerometer event "

1, iOS5.0 before, you can use Uiacceleration to monitor the accelerometer event.

2, after the Bug iOS5.0, Uiaccelerometerdelegate has been depreacated, as follows:

  

Deprecated is not to say, but it means that in the future version will be deleted, so if you do not want to update knowledge, use Uiaccelerometer bar. A more insured approach is to use a timer to check for uiacceleration, which is not dependent on this delegate callback.

3, for iOS4.0 and above, we recommend using Cmmotionmanager to monitor the accelerometer event. There are several methods involved:

  

  

4, in fact, Cmmotionmanager is also quite simple, the method of accelerometer is so few.

Gyroscope (sensor) and accelerometer:

The accelerometer and gyroscope values are accessed through the core motion framework, which provides the Cmmotionmanager class. All the data provided by this class is used to describe how the user moves the device.

The application creates an Cmmotionmanager instance and then uses it in one of the following modes:

1 It can execute some code when the action occurs

2 It can monitor a continuously updated structure at all times, allowing you to access the latest values at any time.

Use of gyroscopes (sensors) and accelerometers:

The Label property inspector's lines is changed to 0 and the height is increased.

#import <UIKit/UIKit.h>

@interface Viewcontroller:uiviewcontroller

@property (weak,nonatomic) iboutlet UILabel *accelerometerlabel;
@property (weak,nonatomic) iboutlet UILabel *gyroscopelabel;

@end

. m File:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface Viewcontroller ()

@property (strong,nonatomic) Cmmotionmanager *motionmanager;
The first of the @property (strong,nonatomic) Nsoperationqueue *queue;//
@property (strong,nonatomic) Nstimer *updatetime;

@end

@implementation Viewcontroller

-(Nsuinteger) supportedinterfaceorientations{

return uiinterfaceorientationmaskportrait;
}
-(void) Viewwillappear: (BOOL) animated{

[Super viewwillappear:animated];

[Self.motionmanager startaccelerometerupdates];
[Self.motionmanager startgyroupdates];

Self.updatetime = [Nstimer scheduledtimerwithtimeinterval:1.0/10.0 target:self selector: @selector (Updatedisplay) Userinfo:nil Repeats:yes];
}
-(void) Viewdidappear: (BOOL) animated{
[Super viewdidappear:animated];

[Self.motionmanager stopaccelerometerupdates];
[Self.motionmanager stopgyroupdates];
[Self.updatetime invalidate];
Self.updatetime = nil;
}
-(void) updatedisplay{
if (self.motionManager.accelerometerAvailable) {

Cmaccelerometerdata *accelerometerdata = Self.motionManager.accelerometerData;
Self.accelerometerLabel.text =[nsstring stringwithformat:@ "accelerometer\n---\ n" "x:%+.2f\ny:%+.2f\nz:%+.2f", ACCELEROMETERDATA.ACCELERATION.X,ACCELEROMETERDATA.ACCELERATION.Y,ACCELEROMETERDATA.ACCELERATION.Z];
}

if (self.motionManager.gyroAvailable) {

Cmgyrodata *gyrodata = Self.motionManager.gyroData;
Self.gyroscopeLabel.text = [nsstring stringwithformat:@ "gyroscope\n---\ n" "x:%+.2f\ny:%+.2f\nz:%+.2f", GYRODATA.ROTATIONRATE.X,GYRODATA.ROTATIONRATE.Y,GYRODATA.ROTATIONRATE.Z];
}

}
-(void) viewdidload{
[Super Viewdidload];

Self.motionmanager = [[Cmmotionmanager alloc]init];//Action Manager initialization

if (self.motionManager.accelerometerAvailable) {
Self.motionManager.accelerometerUpdateInterval = 1.0/10.0; 0.1s Update Once
}else{
Self.accelerometerLabel.text = @ "This device has no accelerometer.";

}

if (self.motionManager.gyroAvailable) {
Self.motionManager.gyroUpdateInterval = 1.0/10.0;
}else{
Self.accelerometerLabel.text = @ "This device has no Gyroscope.";
}


/*
Self.queue = [[Nsoperationqueue alloc]init];

if (self.motionManager.isAccelerometerAvailable) {//Determine if there is an accelerometer

Self.motionManager.accelerometerUpdateInterval = 1.0/10.0; 0.1s Update Once
Tell the action Manager to start reporting the accelerometer update
[Self.motionmanager startAccelerometerUpdatesToQueue:self.queue withhandler:^ (Cmaccelerometerdata * _nullable Accelerometerdata, Nserror * _nullable error) {

NSString *labeltext;

if (Error) {

[Self.motionmanager stopaccelerometerupdates];
LabelText = [NSString stringwithformat:@ "Accelerometer encounted error:%@", error];
}else{

LabelText = [nsstring stringwithformat:@ "accelerometer\n---\ n" "x:%+.2f\ny:%+.2f\nz:%+.2f", ACCELEROMETERDATA.ACCELERATION.X,ACCELEROMETERDATA.ACCELERATION.Y,ACCELEROMETERDATA.ACCELERATION.Z];
}

Dispatch_async (Dispatch_get_main_queue (), ^{

Self.accelerometerLabel.text = LabelText;
});

}];
}else {
Self.accelerometerLabel.text = @ "This device has no accelerometer.";

}



if (self.motionManager.gyroAvailable) {//Judging there is no gyroscope

Self.motionManager.gyroUpdateInterval = 1.0/10.0;
[Self.motionmanager startGyroUpdatesToQueue:self.queue withhandler:^ (cmgyrodata * _nullable Gyrodata, Nserror * _ Nullable error) {

NSString *labeltext;

if (Error) {
[Self.motionmanager stopgyroupdates];
LabelText = [NSString stringwithformat:@ "Gyroscope encounted error:%@", error];
}else{
LabelText = [nsstring stringwithformat:@ "gyroscope\n---\ n" "x:%+.2f\ny:%+.2f\nz:%+.2f", gyrodata.rotationrate.x, GYRODATA.ROTATIONRATE.Y,GYRODATA.ROTATIONRATE.Z];

}

Dispatch_async (Dispatch_get_main_queue (), ^{

Self.gyroscopeLabel.text = LabelText;
});
}];
}else{
Self.accelerometerLabel.text = @ "This device has no Gyroscope.";
}
*/

}
@end

iOS new accelerometer event (gyroscope and accelerometer)

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.