2

The Twitter app is a tab bar app on the iPhone. Nothing in any of the tabs will rotate, yet, when you click on a link from a tweet, the view controller that is pushed on top of it IS allowed to rotate. The only rotations I have ever doe is from tilting the device, landscape or portrait, but I don't understand how to use 2d transformations and animations to rotate views.

How do you rotate any view with that's a subclass of UIView?

2
  • I don't understand. Do you want to be able to rotate the pushed view? When do you want to rotate it? Sorry, myy english is not very good Commented Sep 25, 2012 at 21:22
  • Hey! You asked how to rotate uiviews using animations and transformation. And you accepted the answer of sub classing tabbar. Could you please review your question and the accepted answer? Commented Sep 28, 2012 at 20:47

7 Answers 7

14

You can use UIView animations and Transform methods. For rotating 90 degrees:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

imageView.transform = CGAffineTransformMakeRotation(M_PI_2);

[UIView commitAnimations];
0
2
+50

Hey I had a similar problem with rotation. I had a tabbar app that I wanted to remain portrait but have a video player that allowed all rotations.

I managed to do this fairly easily by sub-classing the tabbar and only allowing portrait rotation while attaching the video player to the Root View Controller that allowed all rotations. Check out my original post here. Hope its of some help!

0

Check this tutorial, i think this is what you wanted to do
http://www.raywenderlich.com/9864/how-to-create-a-rotating-wheel-control-with-uikit
http://www.raywenderlich.com/5478/uiview-animation-tutorial-practical-recipes

0

This is the best and perfect work for me and i think this the right answer.

Just Give Number How many Times You Want To Rotate In Place Of 50 In toValue

    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    NSNumber *currentAngle = [CircleView.layer.presentationLayer valueForKeyPath:@"transform.rotation"];
    rotationAnimation.fromValue = currentAngle;
    rotationAnimation.toValue = @(50*M_PI);
    rotationAnimation.duration = 50.0f;             // this might be too fast
    rotationAnimation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it
    [CircleView.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"];
-1

Ref From How to implement UIViewController rotation in response to orientation changes?

I do this by having a root view controller (this could be a UITabBarController) and in it's viewDidLoad method i subscribe to rotation events:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didRotate:)
                                                  name:@"UIDeviceOrientationDidChangeNotification" 
                                                  object:nil];

Then in the didRotate: method i look at which view controller is visible when the rotation happened, and what orientation the phone is in:

- (void) didRotate:(NSNotification *)notification { 
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

/*
DEVICE JUST ROTATED TO PORTRAIT MODE

 orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown

 */
if(orientation == UIDeviceOrientationPortrait) {





/*
DEVICE JUST ROTATED TO LANDSCAPE MODE
 */
}else if(orientation == UIInterfaceOrientationLandscapeLeft ||
    orientation == UIInterfaceOrientationLandscapeRight) {






}

}

Within that didRotate: you can look at which is the visible viewController and do what you want from there.

I present a modal view controller in landscape mode when a particular view controller is visible and the phone is rotated into landscape. If any other view controller is visible, i ignore the event.

I force my modal view controller to display in landscape mode in its viewWillAppear method - i can give anyone this code if they want it.

Hope this helps.

-1

You are able to implement the shouldAutorotateToInterfaceOrientation method. In the Twitter app i am guessing they allow only portrait orientation for the tabBarController, but allow landscape in the modalViewController using this method. look it up in the documentation, it's pretty simple I think. I hope this answers your question!

6
  • As I had mentioned with other answers, you cannot rotate a modal view controller if its parent controller (in this case the tab bar controller) is not allowed to rotate.
    – user717452
    Commented Sep 11, 2012 at 15:17
  • I just installed the twitter app. They are not using modal view controllers. they are just pushing new view controllers on top of the old one, and in MY experience, you are completely allowed to rotate view controllers when they are pushed, not presented Commented Sep 11, 2012 at 15:30
  • From my table view I use: [self.navigationController pushViewController:_webViewController animated:YES]; And in _webViewController I have shouldAutoRotate return to YES, and it won't rotate.
    – user717452
    Commented Sep 11, 2012 at 17:01
  • Could you post some sample code? Doing this has worked countless times in the past for me, maybe I could help figure it out Commented Sep 11, 2012 at 17:13
  • You gave me one line... I need to see how you implement shouldAutoRotateToInterfactOrientation and the code in your navigation controller that is setting up the _webViewController. I'm just trying to help bro Commented Sep 11, 2012 at 17:21
-2

@Mangesh-Vyas has some good information but doesn't quite answer the question (as I read it). The reason that the views that Twitter (among other apps) presents can rotate, when the tabbar or navbar cannot, is because they are presented modally. Views which are presented modally aren't constrained to the allowable orientations of the controller which presented them.

Edit: Perhaps you are expecting a stock UIViewController to rotate. If so then you will be disappointed, as only (to the best of my knowledge) custom subclasses can be made to rotate to arbitrary interfaceOrientations. The following code will do the trick:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation 
{ 
    return YES;
}

Edit 2: Here's the code that makes the whole thing go:
AppDelegate.m

#import "RotateViewController.h"

@interface AppDelegate () {
    UINavigationController* nc;
}

- (void)pushVC;
- (void)openVC;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIViewController* vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor blueColor];
    vc.title = @"Root";

    UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame = CGRectMake(50, 150, 220, 40);
    [b.titleLabel setText:@"Push VC"];
    [b addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
    [vc.view addSubview:b];

    b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame = CGRectMake(50, 200, 220, 40);
    [b.titleLabel setText:@"Modal VC"];
    [b addTarget:self action:@selector(openVC) forControlEvents:UIControlEventTouchUpInside];
    [vc.view addSubview:b];

    nc = [[UINavigationController alloc] initWithRootViewController:vc];
    UITabBarController* tc = [[UITabBarController alloc] init];
    [tc addChildViewController:nc];

//  uncomment this line to see this work in a nav controller
//  [self.window setRootViewController:nc];
    [self.window setRootViewController:tc];

    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)openVC {
    RotateViewController* rc = [[RotateViewController alloc] init];
    [nc presentModalViewController:rc animated:YES];
}

- (void)pushVC {
    RotateViewController* rc = [[RotateViewController alloc] init];
    [nc pushViewController:rc animated:YES];
}

RotateViewController is a stock subclass of UIViewController with the shouldAutorotate function from above.

Nota bene UITabBarControllers can rotate to a given interface orientation if, and only if, every one of their child view controllers returns YES for that orientation. UINavigationViewControllers can rotate to a given interface orientation if their top view controller returns YES for that interface orientation.

4
  • I beg to differ on this one. I have already tried present Modal Controller, and set the Bool on autoRotation to YES, and it still will not rotate.
    – user717452
    Commented Sep 11, 2012 at 14:21
  • I just verified that you can in fact rotate a modal viewcontoller, see my edits.
    – Clay
    Commented Sep 11, 2012 at 16:48
  • Also, if the view is the top view on a nav controller's stack it can be rotated.
    – Clay
    Commented Sep 11, 2012 at 16:57
  • Alright, then go to a Tab Bar Controller view, punch in some code to present a modal view controller, and tell that view it can rotate, while telling the tab bar controller it can't, and send me the screen shots of your success. It doesn't work.
    – user717452
    Commented Sep 11, 2012 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.