如何更改按钮点击的方向,如youTube应用程序?

时间:2013-08-23 12:52:19

标签: cocoa-touch orientation

如何更改youTube应用的方向。

enter image description here

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

2 个答案:

答案 0 :(得分: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);

    }

}

答案 1 :(得分: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;
   }];

}

此代码是在未经测试的情况下编写的,只是为了让您了解它是如何进行的。