IOS gravity sensing

Source: Internet
Author: User

I wrote an example of gravity sensing yesterday. I think this example is useful. I will share it with you:

1) Obviously, after ios4, you can use the coreMotion framework to add UIAccelerator for backward compatibility,

[Html]
# Import <Foundation/Foundation. h>
# Import <CoreMotion/CoreMotion. h>
@ Protocol IFAccelerometerDelegate <NSObject>
-(Void) accelerateWithX :( NSNumber *) x withY :( NSNumber *) y withZ :( NSNumber *) z withTimeInterval :( NSTimeInterval) timeInterval;
@ End
@ Interface IFAccelerometer: NSObject <UIAccelerometerDelegate>
{
UIAccelerometer * _ accelerometer;
CMMotionManager * _ motionManager;
Id <IFAccelerometerDelegate> _ delegate;
}
+ (Id) specify accelerometer;
-(Void) addOberser :( id) oberserer;
-(Void) removeObserver;
 
@ End

CMMotionManager will be the Object we use and can be used to monitor gravity!
At the same time, we cannot directly use this class where we need to monitor gravity sensing. This type of coupling is serious and is not conducive to reuse. As you can see in the code, I will define a signleton and call back the gravity change event to its proxy.

2. Next, define the function. This is very simple and you can directly paste the code.

[Html]
# Import "IFAccelerometer. h"
Static IFAccelerometer * accelerometerInstance = nil;
@ Implementation IFAccelerometer
+ (Id) Configure Accelerometer
{
If (! AccelerometerInstance ){
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
AccelerometerInstance = [[self class] alloc] init];
});
}
Return accelerometerInstance;
}
 
-(Id) init
{
Self = [super init];
If (self ){
# Ifdef _ IPHONE_5_0
_ MotionManager = [[CMMotionManager alloc] init];
If (_ motionManager. accelerometerAvailable ){
[_ MotionManager setAccelerometerUpdateInterval: 1/60. f];
NSOperationQueue * operationQueue = [NSOperationQueue mainQueue];
[_ MotionManager startAccelerometerUpdatesToQueue: operationQueue withHandler: ^ (CMAccelerometerData * data, NSError * error)
{
If ([_ delegate respondsToSelector: @ selector (accelerateWithX: withY: withZ: withTimeInterval :)])
{
NSNumber * x = [NSNumber numberWithDouble: data. acceleration. x];
NSNumber * y = [NSNumber numberWithDouble: data. acceleration. y];
NSNumber * z = [NSNumber numberWithDouble: data. acceleration. z];
[_ Delegate accelerateWithX: x withY: y withZ: z withTimeInterval: data. timestamp];
}
}
];
 
}
# Else
# Ifdef _ IPHONE_4_0
_ Accelerometer = [UIAccelerometer sharedAccelerometer];
[_ Accelerometer setUpdateInterval: (1/60. 0f)];
_ Accelerometer. delegate = self;
# Endif
# Endif
 
}
Return self;
}
 
-(Void) addOberser :( id) oberserer
{
_ Delegate = oberserer;
}
 
-(Void) removeObserver
{
_ Delegate = nil;
}
 
-(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration
{
If ([_ delegate respondsToSelector: @ selector (accelerateWithX: withY: withZ: withTimeInterval :)])
{
NSNumber * x = [NSNumber numberWithDouble: acceleration. x];
NSNumber * y = [NSNumber numberWithDouble: acceleration. y];
NSNumber * z = [NSNumber numberWithDouble: acceleration. z];
[_ Delegate accelerateWithX: x withY: y withZ: z withTimeInterval: acceleration. timestamp];
}
 
}
3. Using ViewController as an example to describe how to use gravity sensing
[Html]
-(Void) viewDidLoad
{
[Super viewDidLoad];
[[IFAccelerometer using accelerometer] addOberser: self];
_ ImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "33.png"];
_ ImageView. frame = CGRectMake (0, 0, KIMAGEWIDTH, KIMAGEHEIGHT );
_ ImageView. center = self. view. center;
[Self. view addSubview: _ imageView];

}

_ ImageView is a member of UIImageView. Its width and height are macro defined by me.
Note: I added a gravity sensor proxy to the header file. This line

[[IFAccelerometer using accelerometer] addOberser: self];

It indicates that I use the gravity sensor data in this viewController.

Okay. Now it's time to complete the callback and paste the code.

[Html]
-(Void) accelerateWithX :( NSNumber *) x withY :( NSNumber *) y withZ :( NSNumber *) z withTimeInterval :( NSTimeInterval) timeInterval
{
 
Float deceleration = 0.4f;
Float sensiti.pdf = 6.0f;
Float maxVelocity = 100366f;

Velocity. x = velocity. x * deceleration + [x doubleValue] * sensitiation;
Velocity. y = velocity. y * deceleration + [y doubleValue] * sensitiation;

If (velocity. x> maxVelocity ){
Velocity. x = maxVelocity;
} Else if (velocity. x <-maxVelocity ){
Velocity. x =-maxVelocity;
}

If (velocity. y> maxVelocity ){
Velocity. y = maxVelocity;
} Else if (velocity. y <-maxVelocity ){
Velocity. y =-maxVelocity;
}

CGPoint pos = _ imageView. center;
Pos. x + = velocity. x;
Pos. y-= velocity. y;

Float imageWidthHalved = KIMAGEWIDTH * 0.5f;
Float leftBorderLimit = 0.0f;
Float rightBorderLimit = 0.0f;
If (imageWidthHalved> self. view. frame. size. width/2.0f ){
LeftBorderLimit = self. view. frame. size. width-imageWidthHalved;
RightBorderLimit = imageWidthHalved;
}
Else
{
LeftBorderLimit = imageWidthHalved;
RightBorderLimit = self. view. frame. size. width-imageWidthHalved;
}


 
Float imageHeightHalved = KIMAGEHEIGHT * 0.5f;
Float topBorderLimit = 0.0f;
Float bottomBorderLimit = 0.0f;
If (imageHeightHalved> self. view. frame. size. height/2.0f ){
TopBorderLimit = self. view. frame. size. height-imageHeightHalved;
BottomBorderLimit = imageHeightHalved;
}
Else
{
TopBorderLimit = imageHeightHalved;
BottomBorderLimit = self. view. frame. size. height-imageHeightHalved;
}


If (pos. x <leftBorderLimit ){
Pos. x = leftBorderLimit;
Velocity = CGPointZero;
} Else if (pos. x> rightBorderLimit ){
Pos. x = rightBorderLimit;
Velocity = CGPointZero;
}

If (pos. y <topBorderLimit ){
Pos. y = topBorderLimit;
Velocity = CGPointZero;
} Else if (pos. y> bottomBorderLimit ){
Pos. y = bottomBorderLimit;
Velocity = CGPointZero;
}

_ ImageView. center = pos;
}

The above operations such as boundary processing are very simple. I will not describe them one by one.

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.