I had asked this question once before but has had 0 attention and the person helping me has not responded in weeks, so please forgive me but I still do need help.
Im working with CGAffineTransformMakeRotation to flip a UILabel 180 degrees, I want the rotation based on UIOrientationPortrait and UIOrientationPortraitUpsideDown. I get 1/2 the result: when users flip to upside down (from portrait) the label transforms 180 and turns upside down as well (still facing the home button [Important])
BUT
when I rotate it back to Portrait the label stays in the flipped rotation state and does not stay with the home button. Thats what I need help with....
Here is the code i have:
#define degreesToRadian(x) (M_PI * (x) / 180.0)
...
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
TranslateLabel.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
}
You are rotating it upside down no matter what the orientation is. If you want it to rotate differently depending on the orientation, you have to do something like this:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
switch(toInterfaceOrientation){
case UIInterfaceOrientationPortrait:
TranslateLabel.transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortraitUpsideDown:
TranslateLabel.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
break;
}
}
That way, it will rotate the label upside down when the device is upside down and it will return it back to normal when the device is on portrait orientation.
I think you want to switch on toInterfaceOrientation, setting the transform as you have above if the orientation is UIInterfaceOrientationPortraitUpsideDown and to CGAffineTransformIdentity when it's UIInterfaceOrientationPortrait. (This assumes you don't allow rotations to the other orientations.)
Related
Why doesn't this UIView layout code work as I want?
Background:
I have a custom UIView I have in my UIViewController
The custom UIView has a clock background imageview and an hourhand imageview
After introducing some code (see below) to try to resize the hour hand for an orientation change I'm getting stuck.
The code below really stuffs things up - the border outlike of the hour glass hand is way off with this code
Any ideas what I'm doing wrong - there's obvious some assumption or misunderstanding I have for it to screw up like this...
Code called by UIViewController (in "didRotateFromInterfaceOrientation" method) to my custom view:
// Centre image
self.hourHandImageView.center = self.hourFaceUIImageView.center;
// Resize to cater for either Portrait or Landscape orientation
CGSize currSize = self.hourFaceUIImageView.frame.size;
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
CGSize newHourHandViewSize = CGSizeMake(newHourHandImageheightWidth, newHourHandImageheightWidth);
CGRect newRect = self.hourHandImageView.frame;
newRect.size = newHourHandViewSize;
self.hourHandImageView.frame = newRect;
PS. Without the "Resize to cater for either Portrait or Landscape orientation" code the hour hand correctly stays centered where it should (noting I use transformation to turn it around). There all I really need to do is resize the hour hand appropriately after an orientation change, noting the hour background image is smaller in the landscape mode.
First, how can you tell if you are in portrait or landscape orientation? With this (?):
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
What i would advice you, is to do something like this:
-
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[self reOrderToPortrait];
} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
[self reOrderToLandScape];
}
}
Then on each method, you should define the new frames for your views. Start by removing your autosizing from your views, because you will define the new frames, and you dont want that to interfere with. You could also define some mesures like this:
#define hourHandImageViewPortrait CGPointMake(...,...)
#define hourHandImageViewLandScape CGPointMake(..., ...)
Ended up with doing this:
Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape? Works superb!
I have made an image in Photoshop which I want to use as background for my info-screen in my iPad application. The image contains text and some icons also. Around the image I have a border which is green.
The effect I am trying to achieve is:
When the user goes from the portrait orientation to landscape orientation I want the image (just the frame and the icons) to rotate 90 degrees so the image appear in landscape mode, instead of having a portrait view of the frame in landscape. The text and icons are decoupled (different layers which I have organized in different UIImageView's)they shall rotate 90 degrees.
What I have done already is the following:
Experimented a bit with this method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
and tried to do this:
self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);
which I think will rotate the btnFacebook property 90 degrees to the right (correct me if I am wrong) since it specifies positive radians. I can't seem to get it to work correctly though. Should'nt this rotate the button 90 degrees around its center coordinate in the frame? That wouldn't cause a change in the position of the button (the button is square)?
EDIT
Made an image:
As the image shows the image background (some custom graphics on it which looks good in both orientations) goes from portrait to landscape and rotates so it does not appear as portrait in landscape, the icons are decoupled from the background so they need to rotate as well because they need to be in the right orientation (it is social icons). The text however are on the same position, it only rotates 90 degrees without repositioning.
I understand your question as that you are trying to not to rotate the interface as whole but to rotate the purple and red squares individually.
I created a UIViewController that resembles your layout.
The black square is a UIView and the white square is there only so that I can tell when the black view rotates. This view is wired to view1 property on the controller.
There are 4 buttons that are wired to btnx (x runs 1 through 4) properties.
Since I no longer want to auto rotate the interface I only support the portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
To do the rotation manually, I added a method to the ViewController. It determines the angle it needs to rotate the components from portrait to current orientation, creates a rotation transform and applies it to all outlets.
- (void)deviceDidRotate:(NSNotification *)notification
{
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
double rotation = 0;
UIInterfaceOrientation statusBarOrientation;
switch (currentOrientation) {
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
return;
case UIDeviceOrientationPortrait:
rotation = 0;
statusBarOrientation = UIInterfaceOrientationPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = -M_PI;
statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
rotation = -M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
break;
}
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.btn1 setTransform:transform];
[self.btn2 setTransform:transform];
[self.btn3 setTransform:transform];
[self.btn4 setTransform:transform];
[self.view1 setTransform:transform];
[[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
} completion:nil];
}
The last thing to do is to get the OS to call my method. To achieve that I added the following code to application:didFinishLaunchingWithOptions: in the AppDelegate.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:#selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
I am not sure whether this is exactly what you wanted but I believe it is at least similar so you can get some ideas from it how to solve your problem. I can provide source code for a working iPad application that I created to illustrate this.
Hai.
In my iphone app i won't to change my tabbar orientation to landscape or portrait with out rotating the device, such that when i am pressing rotate button whole tabbar controller need to rotate for either landscape or portrait mode.
can any one please give me a suggestion.
If you don't want to change orientation, try changing the frame.
void convertPortraitToLandscap()
{
CGPoint temp;
temp = [touch locationInView:self];
touchPoint.x = temp.y;
touchPoint.y = 320 - temp.x;
}
The above code convert points from portrait to landscape... similarly try changing the frame of the view.
I'm just trying to write a view based Application, which only uses the landscape Orientation.
Using this Code:
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
in the applicationDidFinishedLaunching method the Application starts in landscape orientation.
The problem is, that if I create a landscape view in InterfaceBuilder, the view does not get shown up the way I created it... I need to create the view in portrait mode and imagine how it looks when it is in portrait mode (hope you understand what I mean?) - I insert already rotated images.
So, I got everything work now, but the InfoButton doesn't rotate (as the other buttons, but the others are already rotated images)...
Whats my fault? How to I tell the Application to use my View, which I designed in Landscape orientation?
Thanks so much.
regards
You may have to rotate your view (button) dependent on orientation:
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
CGAffineTransform transform;
switch (orientation) {
case UIDeviceOrientationPortrait:
transform = CGAffineTransformIdentity;
break;
case UIDeviceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(180*M_PI/180);
break;
case UIDeviceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(90*M_PI/180);
break;
case UIDeviceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(-90.0*M_PI/180);
break;
default:
transform = CGAffineTransformIdentity;
break;
}
[[myView layer] setAffineTransform:transform];
You don't need to create the view in portrait mode and imagine how it looks when it is in portrait mode. Just you need to create your view in your needed mode, then set your project setting to your needed mode. In Xcode on the left view you can see Project Navigator click on the blue icon of project name. Now on the middle view (editor view) you can see the summary tab, on this tab set your needed mode on the iPhone Development Orientations.
I'm trying to create an iPhone application that is always in landscape mode, using the Utility application template. Here's what I did:
Create a new iPhone application project, using the Utility Application template
In Interface Builder, rotate all the views 90 degrees.
In Interface Builder, add a label to the middle of the MainView. Stretch it all the way across the view, set the alignment to centered, and set the autosizing springs so that it can stretch horizontally.
In Info.plist, add the key "UIInterfaceOrientation" with value "UIInterfaceOrientationLandscapeRight"
In the controller classes, change the shouldAutorotateToInterfaceOrientation methods to "return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);"
Run the app.
When I launch my app, it comes up in landscape orientation, but the main view only covers the top half of the display, and it is stretched horizontally. I get the same results in both the simulator and on an actual device. I've seen it with versions 2.2 and 2.2.1 of the SDK.
I have been able to work around the problem by adding the following step to the above:
Add "self.view.autoresizesSubviews = NO;" to RootViewController's viewDidLoad method after "[super viewDidLoad];".
If I do this, then it works as expected. But this feels like a hack. Why should this be necessary?
I don't think it is a transformation issue. All elements are drawn in the proper orientation and with the proper scaling. The problem seems to be that the bounds rectangles of the main view gets funky. It looks like the height of the main view is being cut by a little more than half, and the width is being increased by about 50%.
If I do the exact same set of steps using the View-based Application template instead of Utility, then everything works as expected. So I'm pretty sure the problem is specific to how a Utility application manages its views.
Anybody understand what's going on here?
I was going to say that setting this key does not rotate your interface; you still need to lay out your content in landscape mode and do the appropriate rotation using CFAffineTransform - see "Launching in Landscape Mode" in iPhone OS Programming Guide. Going to find the reference for you, I found this comment: "To launch a view controller–based application in landscape mode in versions of iPhone OS prior to v2.1, you need to apply a 90 degree rotation to the transform of the application’s root view in addition to all the preceding steps. Prior to iPhone OS 2.1, view controllers did not automatically rotate their views based on the value of the UIInterfaceOrientation key. This step is not necessary in iPhone OS 2.1 and later, however."
So if you're running pre-2.1, you need to add this code to your viewDidLoad method in your view controller. (Otherwise, can you post some code?)
-(void)viewDidLoad
// After loading the view, transform the view so that the co-ordinates are right for landscape
// As described in iPhone Application Programming Guide
// Weird, I'm sure this used to be needed, but it doesn't now. The one in CardScrollViewController is needed though.
{
[super viewDidLoad];
CGAffineTransform transform = self.view.transform;
CGPoint center = CGPointMake(kScreenHeight / 2.0, kScreenWidth / 2.0);
// Set the center point of the view to the center point of the window's content area.
self.view.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
self.view.transform = transform;
}
Jane describes the setting of UIInterfaceOrientation to UIInterfaceOrientationLandscapeRight (or UIInterfaceOrientationLandscapeLeft), and the rotation settings recommended in the documentation, but I used a slightly different block of code (to the same end) in my root view controller:
- (void)loadView
{
UIView *primaryView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
primaryView.backgroundColor = [UIColor clearColor];
// Start in landscape orientation, and stay that way
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGAffineTransform transform = primaryView.transform;
// Use the status bar frame to determine the center point of the window's content area.
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
CGPoint center = CGPointMake(60.0, bounds.size.height / 2.0);
// Set the center point of the view to the center point of the window's content area.
primaryView.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
primaryView.transform = transform;
}
self.view = primaryView;
[primaryView release];
}
In addition to that, I implemented the following delegate method in my root view controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
Finally, I was encountering weird glitches with the Simulator not auto-rotating properly, so I needed to implement the following delegate method in my UIApplicationDelegate:
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
{
// This prevents the view from autorotating to portrait in the simulator
if ((newStatusBarOrientation == UIInterfaceOrientationPortrait) || (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown))
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
After all that, my application was able to start in landscape (right) and stay in that orientation under the 2.0 firmware and in the Simulator.
Try setting the orientation property of the view to Landscape in the nib. This property can be found in 4th tab[Attributes Inspector] of Info View of the UIView under Simulated Metrices.