Cocoa touch 如何像youTube应用程序一样更改按钮点击的方向?

Cocoa touch 如何像youTube应用程序一样更改按钮点击的方向?,cocoa-touch,orientation,Cocoa Touch,Orientation,我怎样才能像youTube应用程序一样改变方向 当我点击这个按钮时,如果视图处于纵向模式,它会在横向模式下旋转,或者如果视图处于横向模式,它会在纵向模式下旋转,当我改变方向时,它也会工作。实际上,Youtube的界面并没有真正旋转,它们只是全屏显示视频层并旋转层 当你旋转设备时,同样的想法也会发生,他们会让视频层填满屏幕,并根据设备的旋转来旋转。Facebook在全屏查看照片时也会这样做,旋转设备只是旋转视图 您可以通过请求UIDevice生成设备符号方向通知来开始监视旋转: [[UIDevic

我怎样才能像youTube应用程序一样改变方向


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

实际上,Youtube的界面并没有真正旋转,它们只是全屏显示视频层并旋转层

当你旋转设备时,同样的想法也会发生,他们会让视频层填满屏幕,并根据设备的旋转来旋转。Facebook在全屏查看照片时也会这样做,旋转设备只是旋转视图

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

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];
然后在-voidOrientionChanged:方法中更改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在全屏查看照片时也会这样做,旋转设备只是旋转视图

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

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];
然后在-voidOrientionChanged:方法中更改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;
   }];

}
这段代码没有经过测试就编写出来了,只是想让您了解如何使用它。

试试这个 首先导入以下内容: 进口

比你的按钮方法使用这个

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);

    }

}
试试这个 首先导入以下内容: 进口

比你的按钮方法使用这个

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);

    }

}

更改UIView纵向和横向的方向..更改UIView纵向和横向的方向..你知道怎么做吗?当我旋转设备时,视图正在旋转,但在按钮上单击如何旋转我不知道的视图。这将是最干净、最好的方法,但实际上不是Youtube,Facebook或其他人正在处理这件事。仅旋转层意味着底层UIViewController仍处于纵向模式。然后,显示系统UIAlertView、UIActionSheet甚至键盘将使它们以纵向显示。要使它们出现在横向中,UIViewController也应该出现在横向中。这就是为什么@sumit mundra的答案实际上是一个更为有效的答案,即使是哈克式的…是的,这是正确的,他们已经改变了它。因为我知道Facebook确实在纵向上显示了警报,尽管视图是横向的。似乎已经解决了这个问题。你知道怎么做吗?当我旋转设备时,视图正在旋转,但在按钮上单击“如何旋转视图”我不知道。这是最干净、最好的方法,但实际上不是Youtube、Facebook或其他人处理它的方式。仅旋转层意味着底层UIViewController仍处于纵向模式。然后,显示系统UIAlertView、UIActionSheet甚至键盘将使它们以纵向显示。要使它们出现在横向中,UIViewController也应该出现在横向中。这就是为什么@sumit mundra的答案实际上是一个更为有效的答案,即使是哈克式的…是的,这是正确的,他们已经改变了它。因为我知道Facebook确实在纵向上显示了警报,尽管视图是横向的。似乎已经解决了这个问题。