UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(dragger.frame.origin.x, dragger.frame.origin.y,dragger.frame.size.width, dragger.frame.size.height)] ;
imgView.image = dragger.image;
overlayView = [[UIView alloc] initWithFrame:CGRectMake(dragger.frame.origin.x,dragger.frame.origin.y,dragger.frame.size.width, dragger.frame.size.height)];
[overlayView addSubview:imgView];
//open the camera
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraOverlayView=overlayView;
[self.navigationController presentModalViewController:self.picker animated:YES];
This works fine in portrait mode but the overlay image does not change while in the landscape mode.
How can i achieve this as I need help on this?
you need to register for change in orientation... like this
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
and then write this function in the same class....
- (void) orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//do stuff
if (orientation==UIDeviceOrientationPortrait) {
imagePickerController.cameraOverlayView = portraitImageView;
}
else if(orientation==UIDeviceOrientationLandscapeLeft)
{
imagePickerController.cameraOverlayView = landscapeImageView;
}
}
Related
i had spend 2 hours finding the correct code to fix my orientation problem
this are my movieplayer code. i need this particular view to be shown in landscape.
in appdelegate i set all orientation and in my rootviewcontroller i set is as portrait only and in my movieplayer view as landscape but not luck. can anybody give me some comment on how to fix the issues please ?
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAll;
}
my rootviewcontroller
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)prepareIntroVideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"designthinking_pt1" ofType:#"mp4"]];
self.playercontroller = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.playercontroller.view setFrame:CGRectMake(0, -20, 320, 480)];
self.playercontroller.movieSourceType = MPMovieSourceTypeFile;
self.playercontroller.scalingMode = MPMovieScalingModeAspectFill;
self.playercontroller.fullscreen = NO;
self.playercontroller.controlStyle = MPMovieControlStyleFullscreen;
//playercontroller.controlStyle = MPMovieControlStyleFullscreen;
self.playercontroller.view.userInteractionEnabled =YES;
self.playercontroller.view.backgroundColor = [UIColor blackColor];
self.playercontroller.shouldAutoplay = NO;
//playercontroller.repeatMode = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playercontroller];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground)
name: UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
[self.playercontroller prepareToPlay];
[self.view addSubview:self.playercontroller.view];
[self.playercontroller setFullscreen:YES animated:YES];
//[self.playercontroller stop];
[self.view sendSubviewToBack:self.playercontroller.view];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft;
}
You can check my example... https://dl.dropboxusercontent.com/u/19438780/testRotate.zip
I have 2 controllers with navigation controller - portrait; for this I "subclassed" nav controller (here is the "rotation" code for portrait)
The 3th controller is landscape - must be presented modal if you want to rotate it - put the rotation code accordingly .
I would look into preferredInterfaceOrientationForPresentation, I spent about a week on this problem a few months ago. I don't remember exactly how i solved it but I do know it had something to do with implementing that method
How can I resize the MPMoviePlayerController dynamically to fix all the size of screens for iPhone/iPad (both portrait & landscape)?
this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithTitle:#"About" style:UIBarButtonItemStyleDone target:self action:#selector(openPopup)];
self.navigationItem.leftBarButtonItem = about;
NSURL *streamURL = [NSURL URLWithString:#"http:/streamUrl/playlist.m3u8"];
_streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview: self.streamPlayer.view];
[self.streamPlayer play];
}
I also tried to add
[self.streamPlayer.view setFrame:self.view.bounds];
but this makes the video is not fix to screen as shown in screenshot
How can I make it fix all screens of all different devices?
Thanks in advance.
The best solution I found is to use the following
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
[self.streamPlayer.view setFrame:CGRectMake(10, 10 ,height-20, width-70)];
i solved this problem in ios 5
as:
AvPlayerLayer *avPlayerLayer =[[AVPlayerLayer alloc]init];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
avPlayerLayer.frame = self.playerView.layer.bounds;
avPlayerLayer.transform = CATransform3DMakeRotation(M_PI / 2.0, 0, 0, 1);
[self.playerView.layer addSublayer:avPlayerLayer];
here is avPlayerLayer.transform you can set it in any direction .
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController) {
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
if ([moviePlayerViewController.moviePlayer respondsToSelector:#selector(setAllowsAirPlay:)]) {
[moviePlayerViewController.moviePlayer setAllowsAirPlay:YES];
}
[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController queue:nil usingBlock:^(NSNotification *notification) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dismissMoviePlayerViewControllerAnimated];
}];
[moviePlayerViewController.moviePlayer play];
}
I'm doing a bit of AR work here and one of the biggest problem right now is one of my UIView will not respond to any frame settings I make to it.
The code is very similar to this. I've made a few major changes to incorporate into my project:
nurse.site11.com/JaredCrawford-iWVU-632a9a0/Libraries/ARKit/AugmentedRealityController.m
"displayView" is used as a custom UIView that is passed to UIImagePickerController's setCameraOverlayView: method.
Now in the initWithViewController method of that AugmentedRealityController.m file, I have tried setting the displayView = [[UIView alloc] initWithFrame:CGRectMakeFrame(0, 0, 480, 320)];
The problem is, even if I set it to displayView = [[UIView alloc] initWithFrame:CGRectMakeFrame(0, 0, 100, 100)]; the view still remains full screen 320 x 480 BUT only for the first time the Augmented Reality view appeared. Subsequent times that the AR view appears, the code works.
I am not sure what the heck is going on and why setting the frame of the displayView not affecting the view at all FOR THE FIRST TIME it is shown.
I'm hoping someone else has experienced this problem before and possibly solved it.
This is my code:
- (id)initWithViewController:(UIViewController *)vc
{
coordinates = [[NSMutableArray alloc] init];
coordinateViews = [[NSMutableArray alloc] init];
latestHeading = -1.0f;
debugView = nil;
[self setRootViewController: vc];
[self setDebugMode:NO];
[self setMaximumScaleDistance: 0.0];
[self setMinimumScaleFactor: 1.0];
[self setScaleViewsBasedOnDistance: YES];
[self setRotateViewsBasedOnPerspective: YES];
[self setMaximumRotationAngle: M_PI / 6.0];
//CGRect screenRect = [[UIScreen mainScreen] bounds];
NSLog(#"inside initWithViewController");
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationPortrait)
{
NSLog(#"Orientation portrait");
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(#"orientation right");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
[self setCurrentOrientation:UIDeviceOrientationLandscapeRight];
}
else if(orientation == UIDeviceOrientationPortraitUpsideDown)
{
NSLog(#"orientation upsidedown");
}
else if(orientation == UIDeviceOrientationLandscapeLeft){
NSLog(#"orientation left");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[self setCurrentOrientation:UIDeviceOrientationLandscapeLeft];
}
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
//NSLog(#"In AR view, device orientation = %#", [[UIDevice currentDevice] orientation]);
//[self setDisplayView: [[UIView alloc] initWithFrame: screenRect]];
//[self setCurrentOrientation:UIDeviceOrientationLandscapeRight];
displayView = [[UIView alloc] initWithFrame:CGRectMake(-80, 80, 480, 320)];
[displayView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5]];
NSLog(#"displayView x = %f", displayView.frame.origin.x);
NSLog(#"displayView y = %f", displayView.frame.origin.y);
NSLog(#"displayView width = %f", displayView.frame.size.width);
NSLog(#"displayView height = %f", displayView.frame.size.height);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[self rootViewController] setView:displayView];
//[[self rootViewController] setView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)]];
//[[rootViewController view] setBackgroundColor:[UIColor blueColor]];
CLLocation *newCenter = [[CLLocation alloc] initWithLatitude:37.41711 longitude:-122.02528];
[self setCenterLocation: newCenter];
[newCenter release];
[self startListening];
[self deviceOrientationUpdate];
startLocation = nil;
i = 0;
objImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
//[objImageView setBackgroundColor:[UIColor blueColor]];
blnHasRotated = NO;
return self;
}
The full code can be seen here: pastebin.com/g0eXJaPq
I have a simple UIWebView that I've added to my UIViewController in the viewDidLoad method:
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.webView];
And it looks great, but when I rotate the phone, the width and height remain the same so now it's too wide for the update view frame. I also tried using self.view.bounds, but it didn't make any difference.
So how do you ensure that a view that is fullscreen on load, stays the same size when rotated? (without using IB)
What you are doing is correct & should work in most scenarios. But since I have no idea about your View Stack. I will suggest a sure shot way -
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
CGRect rect;
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
else
{
//some other dimensions.
}
self.webView = [[UIWebView alloc] initWithFrame:rect];
}
Because web view not called only once it need to be called again
to set the new frame
self.webView = [[UIWebView alloc] initWithFrame:rect];
so you have to register notification in viwewillappear or viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecamePortrait:) name:#"orientationIsPortrait" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecameLandscape:) name:#"orientationIsLandscape" object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsPortrait" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}else {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsLandscape" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
Then implement
-(void)viewBecameLandscape:(id)sender{
if(webview){
[webview.setframe(cgrectmake(x,y,width,height))];
}
}
-(void)viewBecamePortrait:(id)sender{
}
I am creating a application in which I can need to play a video which I do by mpmovieplayer controller .Now i nee to do this for both orientation .But the frame doesnt get set properly .
The code is as follws
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self shouldAutorotateToInterfaceOrientation:[UIDevice currentDevice].orientation];
NSURL *temp = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Floating" ofType:#"mp4"]];
mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:temp];
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
mpviewController.view.backgroundColor = [UIColor clearColor];
mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpviewController.view.userInteractionEnabled= NO;
mpviewController.moviePlayer.fullscreen= YES;
mpviewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[mpviewController moviePlayer] play];
[self.view addSubview:mpviewController.view];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
currentOrientation = interfaceOrientation;
//[self SetInterface];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
mpviewController.view.frame = CGRectMake(0, 0, 1024, 768);
return YES;
}
I dont know where I am wrong.Please let me know for any chages to make in code. So as to get proper orientation.
Ragards Abhi
First
I believe you don't need to resize the mpviewController as it will resize it self alone.
you should only set the -
Second
In the shouldAutorotateToInterfaceOrientation you only set the supported directions in shouldAutorotateToInterfaceOrientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
If it dose not do it you change the view properties in -
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
//do what you want for portrait
}else{
//do what you want for landscape
}
}
you should only return either YES or NO in shouldAutorotateToInterfaceOrientation: method, it's called by framework only to get the information about the supported orientation in your controller, Read the apple documentation for the same.
you need to register for the orientation change notifictaion
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Implement your orientationChanged: method.
//********** ORIENTATION CHANGED **********
- (void) orientationChanged:(NSNotification *)note
{
NSLog(#"Orientation has changed: %d", [[note object] orientation]);
//Put your code here from shouldAutorotateToInterfaceOrientation:
}
Not forget it to remove .
[[NSNotificationCenter defaultCenter] removeObserver:self];
Here are some link
Device orientation - autorotate?
Orientation Changed Notification
No need to change any codings .. simple insert the following codings to the application , it will automatically detect the orientation...
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blackColor]];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"sharkdivertrailer" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184, 200, 400, 300);
[theMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];