I have tried many codes from this website and others too.... But nothing seems to be working for me. Here is my code...
- (IBAction)orientation:(id)sender {
self.currentDeviceOrientation = [[UIDevice currentDevice] orientation];
int cur = self.currentDeviceOrientation;
int port = UIInterfaceOrientationPortrait;
int landl = UIInterfaceOrientationLandscapeLeft;
int landr = UIInterfaceOrientationLandscapeRight;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
NSNumber *value= [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"orientation"];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
}
}
on the click of a button, I want the screen to change from its current orientation i.e, if the screen is in portrait mode, it should change in to landscape and vice-verse.
Try this and change as what you need.
- (IBAction)orientation:(id)sender {
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"landscape left");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"landscape right");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"portrait");
}
}
You need to set orientation as per your needs.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
Related
here is what I'm trying to achieve, it would be to send an action on rotation of the device...
the point is i'm having a 360 player, I would like the video to start only once the device is on landscape. If it's in portrait i'd like to display a message a saying "rotate your device in landscape to start", and when the user rotate the device, the video play.
below is my code so far:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
The problem of this code is than it trigger only on TouchUp Inside in the Sent Event . . . anyway to have the action starting on rotation ?
Thanks for all your help !
EDIT---
TRYIED THE FOLLOWING ASWELL:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
Have you tried
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
Try put your code inside:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
}
and write this methods in your UIViewController. This method is called when the device (interface really) is rotated; if you want do something before the rotation of interface, use willRotateFromInterfaceOrientation
What I am trying to do is make it so that when an iBeacon has been found (when the user is on the home screen), it sends them to another part of the app. Essentially it pushes another view of the app from the storyboard. For example, iBeacon1 pushes View1, iBeacon2 pushes View2, etc, and that's it. Like I said I know that it possible, but because iBeacons are quite new, there isn't much help available.
I would really appreciate it if you could take some time out of your day to help me with this issue that I cannot find the answer to.
Thank you for your time,
Josh
If you are using Storyboards, you could do something like this in your AppDelegate:
var launchedViewControllers = [Int]()
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let navController = self.window?.rootViewController?.navigationController {
for beacon in beacons {
if (beacon.minor == 1) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController1 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController1, animated: true)
}
}
if (beacon.minor == 2) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController2 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController2, animated: true)
}
}
}
}
}
You could also rewrite this to use segues instead of programmatically pushing ViewControllers using a NavigationController but both will work.
pragma mark -
pragma mark -Important Methods
-(void)showSecondController{
if ([_beacon.major isEqual: #3404] && [_beacon.minor isEqual: #10692]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SDOVideoDetailController"];
} else if ([_beacon.major isEqual: #20165] && [_beacon.minor isEqual: #53849]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LRCVideoDetailController"];
} else if ([_beacon.major isEqual: #53088] && [_beacon.minor isEqual: #30487]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CECSVideoDetailController"];
} else if ([_beacon.major isEqual: #55304] && [_beacon.minor isEqual: #59835]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IVideoDetailController"];
} else if ([_beacon.major isEqual: #16004] && [_beacon.minor isEqual: #46054]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CILVideoDetailController"];
} else if ([_beacon.major isEqual: #20214] && [_beacon.minor isEqual: #11696]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ITVideoDetailController"];
} else if ([_beacon.major isEqual: #21559] && [_beacon.minor isEqual: #17557]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PARKVideoDetailController"];
} else if ([_beacon.major isEqual: #59452] && [_beacon.minor isEqual: #21013]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SCVideoDetailController"];
} else if ([_beacon.major isEqual: #42093] && [_beacon.minor isEqual: #49773]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"BAVideoDetailController"];
}
My app will be launched in Landscape,only one view can change to Portrait, in which there is a button,I click this button to change the orientation to Portrait.
It performed as expected on iPad(iOS 8.4,Deployment target: 7.0),and I made an iPhone version,in which I just made some adjust on UI,and it runs on iPhone 6 Plus simulator(Base SDK: 9.1,Deployment target: 7.0),but the button for chagning orientation doesn't work.
And I want to know how to implement this in iOS 9;
The Below is my code:
- (void)changeOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation==UIInterfaceOrientationLandscapeLeft||orientation==UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationLandscapeLeft) {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToPortrait" object:nil];
}else{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationPortrait) {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToLandscapeLeft" object:nil];
}
[UIViewController attemptRotationToDeviceOrientation];
}
This works on iOS9
NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue: orientationValue forKey:#"orientation"];
Go to General tab from app target, and enable all orientations.
For all view which support only landscape orientation implement supportedInterfaceOrientations() and return landscape orientation value.
seen or set tick on General setting.
for i want to build a movie controller,and it`s pushed by a navigationController,how can i set it ,also only this vc can be landscape,others should only be portraitUp,please help,thanks in advance.
Try..
- (void)viewDidAppear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
- (void)viewDidDisappear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
I have a view controller and i have added UIImagePicker as subview. On changing device orientation from portrait to landscape, camera appears to be on half screen rather than full screen. the images in the camera is also rotated 90 degrees. I am supporting device orientation in my app. how to fix this camera orientation issue. Any help is appreciated.
//Code for presenting camera
- (id)init
{
self = [super init];
if (self) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.allowsEditing = YES;
self.cameraPicker = picker;
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );;
//cameraPicker.view.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height );
[self.view addSubview:cameraPicker.view];
}
return self;
}
//Code for supporting device orientation
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setFramesForControls:toInterfaceOrientation];
}
-(void)setFramesForControls :(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
}
}
Try this its working fine
declare this in viewdidload method
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.allowsEditing = YES;
and on button click event do the following
[self presentModalViewController:picker animated:YES];
no matter to check device orientation;
To prevent your image this code will help you-
Define these macros on the top of your ViewController class-
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
Then manage your delegate method in this way-
#pragma mark- Delegate methods of UIImagePickerController Class
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *imgType = [info objectForKey:UIImagePickerControllerReferenceURL];
//NSLog(#"%#",imgType);
imgType = [imgType lastPathComponent];
NSData *imageData;
if (SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
// code here
imageData = UIImagePNGRepresentation(pickedImage);
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
// code here
imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
}
NSError * error = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&error];
if (error != nil)
{
NSLog(#"ERROR: %#", error);
return;
}
[picker dismissViewControllerAnimated:YES completion: nil];
}
It will capture the images according to your requirement.