如何旋转 UIView?

Posted

技术标签:

【中文标题】如何旋转 UIView?【英文标题】:How to rotate UIViews? 【发布时间】:2012-09-11 14:00:52 【问题描述】:

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

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

【问题讨论】:

我不明白。您希望能够旋转推送的视图吗?你想什么时候旋转它?对不起,我的英语不是很好 嘿!您询问了如何使用动画和转换来旋转 uiview。并且您接受了子类标签栏的答案。您能否查看您的问题和接受的答案? 【参考方案1】:

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

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

imageView.transform = CGAffineTransformMakeRotation(M_PI_2);

[UIView commitAnimations];

【讨论】:

【参考方案2】:

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

我很容易做到这一点,方法是对标签栏进行子类化,只允许纵向旋转,同时将视频播放器连接到允许所有旋转的根视图控制器。查看我的原始帖子here。希望对您有所帮助!

【讨论】:

【参考方案3】:

查看本教程,我认为这就是您想要做的http://www.raywenderlich.com/9864/how-to-create-a-rotating-wheel-control-with-uikithttp://www.raywenderlich.com/5478/uiview-animation-tutorial-practical-recipes

【讨论】:

【参考方案4】:

这对我来说是最好的和完美的工作,我认为这是正确的答案。

只需在 toValue 中给出您想要旋转多少次而不是 50 的数字

    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"];

【讨论】:

【参考方案5】:

参考来自How to implement UIViewController rotation in response to orientation changes?

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

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

然后在 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) 









在 didRotate: 中你可以查看哪个是可见的 viewController 并从那里做你想做的事情。

当特定视图控制器可见并且手机旋转为横向时,我会在横向模式下呈现模态视图控制器。如果任何其他视图控制器可见,我将忽略该事件。

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

希望这会有所帮助。

【讨论】:

【参考方案6】:

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

【讨论】:

正如我在其他答案中提到的,如果不允许其父控制器(在本例中为标签栏控制器)旋转,则无法旋转模态视图控制器。 我刚刚安装了 twitter 应用程序。他们没有使用模态视图控制器。他们只是在旧视图控制器之上推新视图控制器,根据我的经验,您完全可以在视图控制器被推送而不是呈现时旋转视图控制器 从我使用的表格视图中:[self.navigationController pushViewController:_webViewController animated:YES];在 _webViewController 我让 shouldAutoRotate 返回 YES,它不会旋转。 你能发布一些示例代码吗?过去这样做对我来说已经无数次了,也许我可以帮助解决这个问题 你给了我一行...我需要看看你如何实现 shouldAutoRotateToInterfactOrientation 以及导航控制器中设置 _webViewController 的代码。我只是想帮助兄弟【参考方案7】:

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

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

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

编辑 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 是 UIViewController 的股票子类,具有上面的 shouldAutorotate 函数。 注意 UITabBarControllers 可以旋转到给定的界面方向,当且仅当它们的每个子视图控制器都针对该方向返回 YES。如果 UINavigationViewControllerstop 视图控制器针对该界面方向返回 YES,则 UINavigationViewControllers 可以旋转到给定的界面方向。

【讨论】:

我希望对此有所不同。我已经尝试过当前的模态控制器,并将 autoRotation 上的 Bool 设置为 YES,它仍然不会旋转。 我刚刚验证了您实际上可以旋转模态视图控制器,请参阅我的编辑。 另外,如果视图是导航控制器堆栈上的顶视图,它可以旋转。 好吧,然后转到标签栏控制器视图,输入一些代码以呈现模式视图控制器,并告诉该视图它可以旋转,同时告诉标签栏控制器它不能,并且把你成功的屏幕截图发给我。它不起作用。

以上是关于如何旋转 UIView?的主要内容,如果未能解决你的问题,请参考以下文章

如何防止 UIView 背景旋转? [复制]

我们如何在 3D 空间中同时旋转 2 个 UIView 平面

如何获得旋转的 UIView 的正确框架(包括正确的位置)

如何在旋转时处理 UIView 圆角(使用 CAShapeLayer)

如何旋转两个 UIView?

使用触摸平滑旋转 UIView