1

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

在此处输入图像描述

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

4

2 回答 2

4

试试这个首先导入这个: #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);

    }

}
于 2013-08-23T14:12:25.890 回答
1

实际上 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;
   }];

}

这段代码是在没有测试的情况下编写的,在这里只是为了让您了解它是如何完成的。

于 2013-08-23T12:58:19.167 回答