New View when device is rotated - iphone

I want to call "presentmodalviewcontroller" when the iPhone / iPod Touch is rotated to landscape mode with a flip animation. When it gets rotated back to portrait, I want to present the first view again, again with the flip animation.
Weren't able to find something working on the web :(
I'm sure you can help me :)
Thanks a lot !
Sebastian

Try listening for the UIDeviceOrientationDidChangeNotification notification:
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
When you get on it, present your controller.

In your UIViewController, accept all interface orientations:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
And your controller will start receiving rotation messages, like:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
and
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Where you can react to rotation and do things like present/dismiss modal viewcontrollers.

Related

Control done button

I have a view with some objects, one of them is a webview where I play a video, this view change the size and the position of the object when the device rotates. The problem occurs when I am playing the video in full screen, if I start watching the video in landscape and while I am watching the video (full screen) I rotate the device and then I hit the done button when I return to the view the objects are not in the position that they should be in that orientation.
I set the position in a function, I call that function in viewdidload and also in willAnimateRotationToInterfaceOrientation but how can I control the rotation while I am watching the video?
You could do something like this:
-(void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerPlaybackDidFinish:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
[self resizeSubviews];
}
So, when the playback finishes, you call your method to resize your subviews, and by the time the player closes, your view should look correct for the orientation.
You could also register for MPMoviePlayerWillExitFullscreenNotification instead, if that more properly fits your needs.

iphone: rotation in mpmovieplayercontroller?

I have a small view on top of an mpmovieplayercontroller. When it is not fullscreen, I am able to adjust the frame of the view to the orientation (when the device rotates). But when I enter fullscreen mode, alothough I manage to present the view, I'm no longer able to maintain the correct frame when the device rotates. It looks like in fullscreen mode, the system simply using CGAffineTransformRotate on the status bar and the moviePlayer. How can I apply this CGAffineTransformRotate to rotate my view correctly?
EDIT:
Ok, so I updated the code to rotate 90% in the X axis after a change in orientation of the mpmovieplayercontroller but the view simply disappears after the first rotate. Here is my code:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
float angle = M_PI / 2; //rotate 180°, or 1 π radians
theView.layer.transform = CATransform3DMakeRotation(angle, 1.0, 0.0, 0.0);
[self changePositionBasedOnOrientation:toInterfaceOrientation]; //here we change the frame of the view
}
I'm not sure if this would necessarily be the correct way to do it (I guess you're adding a subview to the MPMoviePlayerController view?), but what you seem to be after is a callback to when the movie player rotates in fullscreen so you can adjust your own custom views.
You could register for rotation notifications on your custom view, which can then adjust itself every time it receives a callback. You register for rotation notifications as follows:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(layoutViewForOrientation:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
You should remember to endGeneratingDeviceOrientationNotifications and remove your notification observer when you're done with the fullscreen mode.
Ok,
So the best way to overlay the mpmovieplayercontroller in fullscreen is to it like that:
[[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview:mySubview]
It's a bit hacky, but legitimate. It gives you an easy way to deal with rotations (instead of using transformations).

Which method will be called when the iOS device turn into Landscape mode?

I know that I can return YES to support Landscape mode in shouldAutorotateToInterfaceOrientation. But I would like to display a new view, when the user turn the device into landscape mode, how can I do so? thank you.
Observer this event UIDeviceOrientationDidChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationDidChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
A useful technique for just this case is, to present a new modal view controller with a "fade" transition from your current view. You can do this in reaction to UIDeviceOrientationDidChangeNotification as mentioned previously by Aaron.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

How to know when the playControllerBar of moviePlayer will be dismiss?

as the title,when i play movie using moviePlayer I want to know when the playControllerBar will be dismiss,so that i can control my view added in moviePlayer .
Is there anyone know that?
Tell me ,thanks .
I'm not 100% sure if I understand you correctly.
I assume that what you want to do is:
play a movie
add a custom view (overlay) on top of the (running) movie.
assuming what i just wrote down, I think you have to consider the following things:
adding a custom overlay on top of MPMoviePlayerViewController is (as far as I'm concerned) only allowed/possible if the standard player controls are set to none:
[moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleNone];
adding your custom overlay on top of the player is basically the same addSubview procedure as on any other view
[moviePlayerViewController.view addSubview:overlay];
the above code / concept will work on 3.2 and later, as i just read now you're obviously developing for 3.0
rather then deleting the first part of my answer i will now explain how to achieve the same effect on 3.0
on 3.0 it's a bit trickier (as you sure know by now).
MPMoviePlayerController is not a view Controller and works only in fullscreen-mode. Once the movie starts playing, the keyWindow changes! so We make use of that by implementing the following:
1) within your Class that encapsulates the MPMoviePlayerController, start listening to the UIWindowDidBecomeKeyNotification by doing the following:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(keyWindowChanged:)
name: UIWindowDidBecomeKeyNotification
object: nil];
2) withing your keyWindowChanged: Method you can add your overlay, the following snipplet is exactly how I implemented it:
- (void)keyWindowChanged: (id) sender {
//NSLog(#"keyWindowChanged");
[[NSNotificationCenter defaultCenter] removeObserver: self name: UIWindowDidBecomeKeyNotification object: nil];
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayerWindow addSubview: overlayController.view];
[overlayController performSelector:#selector(fadeIn)];
}
again, this only works if the MovieControllMode is "hidden" by doing that:
[newMPController setMovieControlMode: MPMovieControlModeHidden];
I hope I could help.

Change view layout with orientation

I am working on an application where I want the view to change quite a lot when the device is rotated. I know that in Interface Builder it is possible to change the orientation and set up the layout there, however when I then rotate it to portrait, the view is the same as for the landscape. Is there a way to set up the two views independently, perhaps using two nibs?
Cheers,
JP
If you're after switching view controllers on orientation changes, like when the iPod app shows it's coverflow view, the following post will help:
http://rndnext.blogspot.com/2009_06_01_archive.html
I don't know if you can do it in IB, but I know you can do it in code. You need to observe the UIDeviceOrientationDidChangeNotification Notification:
(in viewDidLoad...)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(updateViewOnRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
....
-(void) updateViewOnRotate:(NSNotification *notification) {
// update the views
}