【问题标题】:How to change a orientation on button click like youTube app?如何像youtube应用程序一样更改按钮单击的方向?
【发布时间】:2013-08-23 12:52:18
【问题描述】:

如何像 youtube 应用一样更改方向。

当我点击此按钮时,如果视图处于纵向模式,它会以横向模式旋转,或者如果视图处于横向模式,它会以纵向模式旋转,当我改变方向时它也可以工作。

【问题讨论】:

  • 改变 UIView 纵向和横向的方向..

标签: cocoa-touch orientation


【解决方案1】:

试试这个 首先导入这个: #import <objc/message.h>

比你的按钮方法使用这个

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
        objc_msgSend([UIDevice currentDevice],@selector(setOrientation:),UIInterfaceOrientationLandscapeLeft );
    }else
    {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientation);

    }

}

【讨论】:

    【解决方案2】:

    实际上 Youtube 的界面并没有真正旋转,他们只是将视频层全屏呈现并旋转层。

    旋转设备时也会发生同样的想法,它们使视频层充满屏幕并根据设备旋转进行旋转。 Facebook 在全屏查看照片时也是如此,旋转设备只会旋转视图。

    您可以通过要求UIDevice 生成设备符号方向通知来开始监控旋转:

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

    然后在-(void)orientationChanged: 方法中更改UI:

    - (void) orientationChanged:(NSNotification *)note
    {
       UIDevice * device = note.object;
    
       CGAffineTransform transfrom;
       CGRect frame = self.videoView.frame;   
    
       switch(device.orientation)
       {
           case UIDeviceOrientationPortrait:
           case UIDeviceOrientationPortraitUpsideDown:       
               transfrom = CGAffineTransformIdentity;
               frame.origin.y = 10.0f;
               frame.origin.x = 10.0f;
               frame.size.width = [UIScreen mainScreen] bounds].size.width;
               frame.size.height = 240.0f;
    
           break;
    
           case UIDeviceOrientationLandscapeLeft:
               transfrom = CGAffineTransformMakeRotation(degreesToRadians(90));
               frame.origin.y = 0.0f;
               frame.origin.x = 0.0f;
               frame.size.width =[UIScreen mainScreen] bounds].size.height;
               frame.size.height =[UIScreen mainScreen] bounds].size.width;
           break;
    
           case UIDeviceOrientationLandscapeRight: 
               transfrom = CGAffineTransformMakeRotation(degreesToRadians(-90));
               frame.origin.y = 0.0f;
               frame.origin.x = 0.0f;
               frame.size.width =[UIScreen mainScreen] bounds].size.height;
               frame.size.height =[UIScreen mainScreen] bounds].size.width;
              break;
    
           default:
           return;
           break;
       };
    
    
       [UIView animateWithDuration:0.3f animations: ^{
            self.videoView.frame = frame;
            self.videoView.transform = transfrom;
       }];
    
    }
    

    此代码未经测试而编写,只是为了让您了解如何执行此代码。

    【讨论】:

    • 你知道怎么做吗?
    • 当我旋转设备时,视图正在旋转,但在按钮上单击如何旋转我不知道的视图。
    • 这将是最干净、最好的方法,但实际上不是 Youtube、Facebook 或任何这些人处理它的方式。仅旋转图层意味着底层 UIViewController 仍处于纵向模式。然后,显示系统 UIAlertView、UIActionSheet 甚至键盘将使它们以纵向显示。要让它们以横向显示,UIViewController 也应该以横向显示。这就是为什么@sumit-mundra 的答案实际上是一个更有效的答案,即使 hacky...
    • 是的,这是正确的,他们已经改变了它。因为我知道 Facebook 确实以纵向显示警报,即使视图是横向的。似乎已经解决了这个问题。
    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2022-01-05
    • 2016-08-11
    相关资源
    最近更新 更多