views:

720

answers:

3

Hello guys!

How can I detect the start of a rotation and the end of the rotation on these devices?

EDIT: So after your answers, how can I detect the begining and ending of the orientation change.

A: 

You can use the UIAccelerometer api for this

EDIT to match question clarification:

Use the UIAccelerometer api for this. There is a good sample application called AccelerometerGraph that displays the 3 acceleration vectors on a graph. This should give you a good indication on how a shift in orientation will look like.

Claus Broch
No you can't. The accelerometer doesn't detect rotation.
Marcelo Cantos
It depends whether the device is rotated around an axis going through itself or not. It is true you can't detect it as a direct rotation, but instead each of the x,y,z axis will/may have different acceleration values that might be used for detecting this.
Claus Broch
@Claus, without knowing the orientation _a priori_, it is impossible (in a very deep Einsteinian sense) to distinguish between circular motion and acceleration.
Marcelo Cantos
Argh, since you now brought Einstein into the equation I'm bound to agree with you :-)
Claus Broch
Claus, your edit is just as wrong as your original answer. You can't tell orientation from the accelerometer. Any sequence of accelerometer readings produced by rotating the device can be reproduced by moving the device without rotating it.
Marcelo Cantos
Hmmm... anybody care to tell Apple that thay can not detect the orientation of the iPhone? I know that in thery it is impossible to do this, but let's just assume that you are not sitting in a spacecraft that does a 1g constant acceleration which fools the device to think it's orientated differently...
Claus Broch
Thanks guys! :)
Infinity
A: 

You can't detect rotation on either of these devices. The accelerometer only detects changes in velocity.

You can get a crude measure of orientation from the CLLocationManager class, which reports a heading. This value is slow to update, which limits its usefulness.

Marcelo Cantos
Well, strictly speaking the accelerometer reports the forces influencing the device in the three axis measured in G. So if you have the device laying still flat on a table, you will get a constant z value on the z-axis as -1 G. This does not mean that the device is accelerating with 9.8 m/s^2 through the table :-)
Claus Broch
@Claus, acceleration and gravity are identical concepts. You cannot distinguish one from the other. If the phone were out in space with a tiny little jet pack on its back, pushing it forward at 1 G, the accelerometer would measure -1 G in the Z direction, just as it does in your scenario. This means that in both cases, the phone is accelerating _forward_ at 1 G (not backward). Since the earth is somewhat massive, it twists space-time towards it such that that the 1 G outward acceleration while lying flat on the table keeps the phone at a fixed distance from the earth's center.
Marcelo Cantos
+3  A: 

hi,

You can implement the following two methods in your UIViewController.

To detect a start of a rotation, implement willRotateToInterfaceOrientation:duration: and to detect the end implment didRotateFromInterfaceOrientation: .

i.e.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"I am starting to rotate");
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    NSLog(@"I have finished rotating");
}
deanWombourne
Thanks for you too. :)
Infinity