john
john

Reputation: 1319

How to trigger a function when the iPhone is rotated 90 degrees?

How do I detect the rotation of the iPhone for approximately 90 degrees? Additionally, I don't need/want the screen itself to rotate. I just want to call a function when the phone has been rotated. Thanks!

Upvotes: 5

Views: 4602

Answers (5)

b1gm0use
b1gm0use

Reputation: 11

you should first implement the instance method "shouldAutorotateToInterfaceOrientation" and return YES. and then the two method will be called.

Upvotes: 1

Patricia
Patricia

Reputation: 289

The method names are correct... but they never get called. What would cause that?

Upvotes: 0

JamesSugrue
JamesSugrue

Reputation: 15011

You need to catch the events sent prior to ViewController rotation. Namely:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

and post rotation

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Upvotes: 1

When you rotate the device, your view controller receives a didRotateFromInterfaceOrientation: message, with the old orientation. You can check the interfaceOrientation property to see the current rotation, and do something accordingly.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89192

Your view controller will be sent:

  -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

Implement it to do something when the phone is being rotated

After the rotation is done, you get:

  - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Here it is in the docs (see Handling View Rotations)

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

Upvotes: 8

Related Questions