I want to provide a custom animation when the device rotates, completely overriding the default one. What’s the best way to achieve this?
BTW, the kind of animation I’m planning is:
a) When the device goes into landscape, have a new view slide from the top as if it was falling.
b) When the device goes back to portrait, that view should slide down and dissappear.
Best is subjective and dependent upon your application as a whole.
One fairly straight-forward way of handling the rotation events is telling the system not to and handling them yourself. For your desired effect of what essentially amounts to sliding a (pre-rotated) view in from the side when the device is rotated to the side, this would seem appropriate.
Here is a very basic sample of how to achieve such an effect.
What I have done here is added a
UIViewwith a landscape orientation in the.xiband connected it to anIBOutletnamedsideways. InviewDidLoadI add it as a subview, pre-rotate it, and move it offscreen. I also add self as an observer for the device rotation notifications (Remember to remove yourself later for that notification). And inshouldAutoRo..I indicate that this VC only handles portrait.When the device rotates
NSNotificationCentercallsorientationChange:. At that point if the device is rotated to the left, mysidewaysview will slide in from the right (which seems like it’s sliding down).Obviously for both landscape orientations the code would be more complex. Also you would likely have to mess around with the animation timing to make it feel as if the second view is “falling”