xcode 如何旋转 UIViews?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12371491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:27:39  来源:igfitidea点击:

How to rotate UIViews?

iphoneiosxcodecocoa-touchcore-animation

提问by user717452

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.

Twitter 应用程序是 iPhone 上的标签栏应用程序。任何选项卡中的任何内容都不会旋转,但是,当您单击推文中的链接时,被推到其上的视图控制器允许旋转。我做过的唯一旋转是倾斜设备,横向或纵向,但我不明白如何使用 2d 转换和动画来旋转视图。

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

您如何使用 UIView 的子类旋转任何视图?

回答by Abdullah Umer

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

您可以使用 UIView 动画和 Transform 方法。旋转 90 度:

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

imageView.transform = CGAffineTransformMakeRotation(M_PI_2);

[UIView commitAnimations];

回答by bennythemink

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.

嘿,我在旋转方面遇到了类似的问题。我有一个 tabbar 应用程序,我想保持纵向,但有一个允许所有旋转的视频播放器。

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!

通过对选项卡栏进行子类化并仅允许纵向旋转,同时将视频播放器附加到允许所有旋转的根视图控制器,我设法相当容易地做到了这一点。在这里查看我的原始帖子。希望它的一些帮助!

回答by Hardik Vyas

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

只需给出您想要旋转多少次来代替 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"];

回答by Clint Warner

You are able to implement the shouldAutorotateToInterfaceOrientationmethod. 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!

您可以实现shouldAutorotateToInterfaceOrientation方法。在 Twitter 应用程序中,我猜他们只允许 tabBarController 的纵向,但允许使用此方法在 modalViewController 中横向。在文档中查找它,我认为这很简单。我希望这回答了你的问题!

回答by Mangesh

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

参考 如何实现 UIViewController 旋转以响应方向变化?

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:

我通过拥有一个根视图控制器(这可能是一个 UITabBarController)并在它的 viewDidLoad 方法中订阅旋转事件来做到这一点:

[[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:

然后在 didRotate: 方法中,我查看旋转发生时哪个视图控制器可见,以及手机处于哪个方向:

- (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.

在 didRotate: 中,您可以查看哪个是可见的 viewController 并从那里执行您想要的操作。

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.

我强制我的模态视图控制器在它的 viewWillAppear 方法中以横向模式显示 - 如果他们想要,我可以给任何人这个代码。

Hope this helps.

希望这可以帮助。

回答by Clay

@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.

@Mangesh-Vyas 有一些很好的信息,但并没有完全回答这个问题(在我读到的时候)。当标签栏或导航栏不能时,Twitter(以及其他应用程序)呈现的视图可以旋转的原因是因为它们是模态呈现的。以模态呈现的视图不受呈现它们的控制器的允许方向的约束。

Edit:Perhaps you are expecting a stock UIViewControllerto 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:

编辑:也许您期望股票UIViewController轮换。如果是这样,那么您会感到失望,因为只有(据我所知)自定义子类可以旋转到任意 interfaceOrientations。以下代码可以解决问题:

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

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

编辑 2:这是使整个事情顺利进行的代码:
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 UIViewControllerwith the shouldAutorotatefunction from above.

Nota beneUITabBarControllerscan rotate to a given interface orientation if, and only if, every one of their child view controllers returns YESfor that orientation. UINavigationViewControllerscan rotate to a given interface orientation if their topview controller returns YESfor that interface orientation.

RotateViewController 是UIViewController具有上述shouldAutorotate功能的股票子类。 当且仅当它们的每个子视图控制器都返回该方向时,

Nota beneUITabBarControllers可以旋转到给定的界面YES方向。 UINavigationViewControllers如果他们的视图控制器返回YES该界面方向,则可以旋转到给定的界面方向。