detect iPad 2x button for iPhone app - iphone

Is there a way to detect that your iPhone app us running 2x/1x on an iPad?
I need to be able to detect the difference in points per inch for my app.

Check the scale property:
[[UIScreen mainScreen] scale]
Here's a handy function:
+(BOOL) screenIs2xResolution {
return 2.0 == [MyDeviceClass mainScreenScale];
}
+(CGFloat) mainScreenScale {
CGFloat scale = 1.0;
UIScreen* screen = [UIScreen mainScreen];
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
scale = [screen scale];
}
return scale;
}
Credits: http://www.markj.net/iphone-4-2x-graphics-scale-ipad/
See also: http://struct.ca/2010/high-res-graphics-in-cocos2d/

Since you cannot register for _UIClassicApplicationWillChangeZoomNotificationName, it seems to be an internal constant, what I did is:
Register for any Notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeZoom:) name:nil object:nil];
And then check the according values:
- (void)changeZoom:(NSNotification*)notification
{
if ([[notification name] isEqualToString:#"_UIClassicApplicationWillChangeZoomNotificationName"])
{
NSLog(#"Zoom changed to %#", [[[notification userInfo] objectForKey:#"_UIClassicIsZoomedUserInfoKeyName"] boolValue] == 0 ? #"1x" : #"2x");
}
}

You can detect the change by registering for the notification _UIClassicApplicationWillChangeZoomNotificationName and then handle the scale change using more or less the method outlined by #magma in his answer. _UIClassicApplicationWillChangeZoomNotificationName will tell you when the "2x"/"1x" button has been used to change the scale.

Related

How to stop statusbar overlayping in ios 7 when rotating view

i am working compatablity modification for ios 7. My application view is rotating for landscape and portiate mode.
But currently in ios 7 status bar is overlayping to my view.in all rotation. I have try with following code but it not work for me.
In my appDelegate file appdidfinishlaunching i set 1 notification as follows:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
This will cold method as follows:
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a){
case 4:
self.window.frame = CGRectMake(0,20,w,h);
break;
case 3:
self.window.frame = CGRectMake(-20,0,w-20,h+20);
break;
case 2:
self.window.frame = CGRectMake(0,-20,w,h);
break;
case 1:
self.window.frame = CGRectMake(20,0,w-20,h+20);
}
}
For the first time when i launch my application status bar was over layping my view.
Secound time when i rotate my iPad at that time it set status bar black ground on the different possition.
Every time I was getting wrong.
Please help me, Am i doing anything wrong in the above.
And if possible please provide me sample code for all rotation to stop status bar overlayping.
You can stop the navigation bar from overlapping by using this code
self.navigationController.navigationBar.translucent = NO;

How to Detect Flip of iPhone?

is there a logic to detect if the user has flipped their phone like from battery side to screen side and vice versa in horizontal plane? I have tried getting raw values to determine if the device is in the horizontal position on both faces but how to detect the whole motion , could someone point me in right direction.
If you take a look at the UIDevice class reference, you'll see the orientation enum. Two of it's values are UIDeviceOrientationFaceDown and UIDeviceOrientationFaceUp. That being said, all you have to do is register an observer to the UIDeviceOrientationDidChangeNotification notification, and upon call you can check the devices current orientation and handle this accordingly.
[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationFaceDown) {
// device facing down
}else if (orientation == UIDeviceOrientationFaceUp) {
// device facing up
}else{
// facing some other direction
}
}];
Be sure to use the following to start generating the device notifications you'll need to observe.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
If you want to get more specific information about the orientation of the device, you'll need to use the Core Motion framework to get gyro data directly. With this, you can track the exact current direction the device is facing in 3D space.
_motionManager = [CMMotionManager new];
NSOperationQueue *queue = [NSOperationQueue new];
[_motionManager setGyroUpdateInterval:1.0/20.0];
[_motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {
NSLog(#"%#",gyroData);
}];

No perfect way to detect device orientation on iPad?

EDIT: The given answer works on the device, but beware it fails on the simulator.
When my iPad starts up, I show a loading label, centered in the middle of the screen. I set its autoresizingMask so it recenters on orientation change.
As the app starts up, the label's text changes, so I want to recenter the label based on its new length. However, the following piece of code doesn't center the label correctly:
- (void) setLabelText:(NSString*)text {
CGSize maximumLabelSize = CGSizeMake(500,20);
CGSize expectedLabelSize = [text sizeWithFont:loadingLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:loadingLabel.lineBreakMode];
loadingLabel.frame = CGRectMake(self.view.frame.size.width/2-expectedLabelSize.width/2,
loadingLabel.frame.origin.y,
expectedLabelSize.width,
loadingLabel.frame.size.height);
loadingLabel.text = text;
}
I also considered checking [[UIDevice currentDevice]orientation], and if the iPad is in landscape mode, then I'd use self.view.frame.size.height to set the xOrigin of the label.
However, if the device is face up or face down, (and not landscape or portrait) then this method fails. I also have a lastOrientation variable in my appDelegate, which remembers if the app is in landscape or portrait, even when face up or face down, based on the device's last known orientation. However, at start-up, this variable isn't necessarily set.
Is there some simple solution I am missing here, so I can resize and center my label?
EDIT: I tried checking UIStatusBarOrientation based on the advice posted, but it doesn't work:
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
|| [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
NSLog(#"landscape");
width = self.view.frame.size.height;
} else {
NSLog(#"portrait");
}
This always logs portrait, at least on start-up, on the simulator.
Check [[UIApplication sharedApplication] statusBarOrientation]
I found a trick to solve the FaceUp orientation issue!!!
Delay the orientation check till AFTER the app has started running, then set variables, view sizes, etc.!!!
//CODE
- (void)viewDidLoad {
[super viewDidLoad];
//DELAY
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(delayedCheck)
userInfo:nil
repeats:NO];
}
-(void)delayedCheck{
//DETERMINE ORIENTATION
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
FACING = #"PU";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
FACING = #"PD";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
FACING = #"LL";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
FACING = #"LR";
}
//DETERMINE ORIENTATION
//START
[self setStuff];
//START
}
-(void)setStuff{
if( FACING == #"PU" ){
//logic for Portrait
}
else
if( FACING == #"PD" ){
//logic for PortraitUpsideDown
}
else{
if( FACING == #"LL"){
//logic for LandscapeLeft
}
else
if( FACING == #"LR" ){
//logic for LandscapeRight
}
}
//CODE
You can addSubviews, position elements, etc. in the 'setStuff' function ... anything that would initially depend on the orientation!!!
:D
-Chris Allinson

Rotating landscape mode

what i did is displaying 10 images as grid and 3 images in portroide mode, and what i did is when ever i rotate the simulator to landscape then i have to display 4 images.It is also displayed using the code
if(self.interfaceorientation == UIIntefaceorientationPortrait) {
[self abc];
else {
[self abclandscape];
}
here abc and abclandscape are two functions it works fine but, it works from initial means form starting if i rotate to landscape mode or portrait mode it works fine .while in the middle if i rotate from landscape to portrait it does 't goes to [self abc] function. how can i solve this?
What you could do is either use the UIViewController delegates, or use the NSNoticationCenter.
I.E. add in your "viewDidLoad":
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
And add the function:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Do one thing
}
else
{
// Do something else
}
}
#mahesh In shouldAutoRototate Method
use
if(self.interfaceorientation == UIIntefaceorientationPortrait||self.interfaceorientation == UIIntefaceorientationPortraitUp..)
{
}
else
{
}

how do I detect the iPhone orientation before rotating

In my program I'm moving things based on rotation, but I'm not rotating the entire view. I'm Using :
static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) didRotate:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self doRotationStuff:orientation: previousOrientation];
previousOrientation = orientation;
}
This works as long as, when the program is launched, the device orientation is portrait, but not if the initial orientation is landscape or upside down, because [self doRotationStuff] makes changes relative to the difference from the previous orientation.
Is there a way to detect the orientation either at launch, or right before the device is rotated?
Depending on your circumstances, a simpler option may be the interfaceOrientation property of the UIViewController class. This is correct before a rotation.
Updated:
So, from the comment discussion, it appears that you can't rely on [UIDevice currentDevice].orientation until the orientation actually changes for the first time. If so, you could probably hack it by getting raw accelerometer readings.
#define kUpdateFrequency 30 // Hz
#define kUpdateCount 15 // So we init after half a second
#define kFilteringFactor (1.0f / kUpdateCount)
- (void)applicationDidFinishLaunching:(UIApplication *)app
{
[UIAccelerometer sharedAccelerometer].updateInterval = (1.0 / kUpdateFrequency);
[UIAccelerometer sharedAccelerometer].delegate = self;
accelerometerCounter = 0;
...
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel
{
// Average out the first kUpdateCount readings
// acceleration_[xyz] are ivars typed float
acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor);
acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor);
acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor);
accelerometerCounter++;
if (accelerometerCounter == kUpdateCount)
{
[self initOrientation];
[UIAccelerometer sharedAccelerometer].delegate = nil;
}
}
- (void)initOrientation
{
// Figure out orientation from acceleration_[xyz] and set up your UI...
}
Original response:
Does [UIDevice currentDevice].orientation return the correct orientation during applicationDidFinishLaunching:? If so, you can set up your initial UI according to that orientation.
If that property doesn't get set until some later time, you might try experimenting with performSelector:afterDelay: to initialize the UI after a small delay.
This code sample is from Kendall's answer below, added here for completeness:
[self performSelector:#selector(getOriented) withObject:nil afterDelay:0.0f];
I'm not sure if a zero-second delay is sufficient -- this means the code for getOriented will run during the first pass through the event run loop. You may need to wait longer for the accelerometer readings to register on UIDevice.
Mort, these answers seem somewhat of a red herring; I can't see why you can't use the following built-in method for a UIViewController class:
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {}
This method gets called automatically after a rotation has occurred (rather than with shouldAutorotateToInterfaceOrientation which only tells you it's about to happen). Handily, the variable 'fromInterfaceOrientation' contains the previous orientation. As the documentation also says, you can assume that the interfaceOrientation property of the view has already been set to the new orientation, so you then have one method with access to the old orientation and the new!
If I've missed something and you've already dismissed being able to use this method, my apologies! It just seems odd that you're creating and storing a variable for the "previous orientation" when it's provided for free in the above method.
Hope that helps!
Use this for the orientation of the UI if you need to determine what way are you pointing.
Not 100% sure this is right but going off the top of my head:
[[[UIApplication sharedApplication] statusBar] orientation]
Here's one way to get the orientation when the app first loads and the UIDeviceOrientation is set to UIDeviceOrientationUnknown. You can look at the transform property of the rotated view.
if(toInterface == UIDeviceOrientationUnknown) {
CGAffineTransform trans = navigationController.view.transform;
if(trans.b == 1 && trans.c == -1)
toInterface = UIDeviceOrientationLandscapeLeft;
else if(trans.b == -1 && trans.c == 1)
toInterface = UIDeviceOrientationLandscapeRight;
else if(trans.a == -1 && trans.d == -1)
toInterface = UIDeviceOrientationPortraitUpsideDown;
else
toInterface = UIDeviceOrientationPortrait;
}
A more complete example on how to obtain device orientation from accelerator readings can be found here
As the solution relies on accelerator readings, it wouldn't work on the simulator, so you'll have to work on the device... still looking myself for a solution that works on the simulator.
In response to your comment, I thought I could better put code here than in a comment (though really Daniel deserves credit here):
in applicationDidFinishLaunching:
[self performSelector:#selector(getOriented) withObject:nil afterDelay:0.0f];
Then you just need the method to call:
- (void) getOriented
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// save orientation somewhere
}