Code! Here’s some objectiveC code I’m playing around with for tilting

– (void)viewDidLoad

{

fRadius = ivRStickMan.frame.size.width / 2;

cDelta = CGPointMake(0.0, 0.0);

cTrans = CGPointMake(0.0, 0.0);

mm = [CMMotionManager new];

if (    mm.isDeviceMotionAvailable) {

mm.deviceMotionUpdateInterval = 1.0/60.0;

[mm startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]

withHandler:^(CMDeviceMotion *motion, NSError *TheError)

{

CMAttitude *currAttitude = motion.attitude;

float roll = currAttitude.roll;

float pitch = currAttitude.pitch;

float yaw = currAttitude.yaw;

tRoll.text = [NSString stringWithFormat:@”%f”, roll];

tPitch.text = [NSString stringWithFormat:@”%f”, pitch];

tYaw.text = [NSString stringWithFormat:@”%f”, yaw];

CMAcceleration cAcceleration = motion.userAcceleration;

tX.text = [NSString stringWithFormat:@”%f”, cAcceleration.x];

tY.text = [NSString stringWithFormat:@”%f”, cAcceleration.y];

tZ.text = [NSString stringWithFormat:@”%f”, cAcceleration.z];

//animate the rotation via the stick man

if (currAttitude.roll > 0.1)

cDelta.x = 2;

else if (currAttitude.roll < -0.1)

cDelta.x = -2;

else

cDelta.x = 0;

if (currAttitude.pitch > 0.1)

cDelta.y = 2;

else if (currAttitude.pitch < -0.1)

cDelta.y = -2;

else

cDelta.y = 0;

[UIView animateWithDuration:0.5

animations:^{

ivRStickMan.transform = CGAffineTransformMakeTranslation(cTrans.x,

cTrans.y);

} ];

cTrans.x = cTrans.x + cDelta.x;

cTrans.y = cTrans.y + cDelta.y;

if (ivRStickMan.center.x + cTrans.x > 320 – fRadius || ivRStickMan.center.x + cTrans.x < fRadius) {

cTrans.x -= cDelta.x;

}

if (ivRStickMan.center.y + cTrans.y > 460 – fRadius || ivRStickMan.center.y + cTrans.y < fRadius) {

cTrans.y -= cDelta.y;

}

//             //don’t leave the screen

//             if (cTrans.x < 0)

//                 cTrans.x = 0;

//             if (cTrans.y < 0)

//                 cTrans.y = 0;

//             if (cTrans.x > 320)

//                 cTrans.x = 320;

//             if (cTrans.y > 480)

//                 cTrans.y = 480;

//

//ROUNDING NUMBERS

//             float roundedValue = round (2.0f * 5) / 2.0f;

//             NSNumberFormatter *MyFormatter = [NSNumberFormatter new];

//             [MyFormatter setMaximumFractionDigits:2];

//             [MyFormatter setRoundingMode:NSNumberFormatterRoundDown];

//             NSString *numStr = [MyFormatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];

}];

}

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

Advertisements