如何更改单个视图的方向而不是ipad中的所有视图?

时间:2013-11-28 05:39:17

标签: objective-c ipad ios7 uiinterfaceorientation

我对IOS更新鲜。在我的iPad应用程序中,我有6个视图。默认情况下,我将所有屏幕的方向固定为横向模式。但是当我打开那个特定的视图时,我的第五个视图必须像肖像模式一样改变,是否有任何编程代码来改变特定的视图方向?你能不能帮我解决这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:0)

1.您需要通过调用

生成DeviceOrientation通知
-(void)beginGeneratingDeviceOrientationNotifications 

UIDevice的方法。

2.在UIViewController中控制你的视图添加通知观察

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateMyUIView:) name:UIDeviceOrientationDidChangeNotification object:nil];

3.in rotateMyUIView metod使用

旋转你的UIView
myView.transform = CGAffineTransformMakeRotation(M_PI/2); // you must check orientation first 

答案 1 :(得分:0)

您可以在您不希望受到影响的特定视图控制器中实现didRotateFromInterfaceOrientation

答案 2 :(得分:0)

在iOS 6之后你可以这样做..

在AppDelegate.h文件中

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    BOOL allowRotation;
}

在AppDelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    allowRotation = NO;  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeDeviceOrientation:) name:@"ChangeDeviceOrientation" object:nil];

    return YES;
}

- (void) changeDeviceOrientation:(NSNotification*)notification {
   if(allowRotation)
   {
       allowRotation = NO;
   }else
   {
       allowRotation = YES;
   }
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSLog(@"Not FullScreen");
    if (allowRotation) {
        NSLog(@"FullScreen");
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}

在您的viewcontroller中(您希望更改方向的viewC)调用此通知

-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDeviceOrientation"   object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDeviceOrientation"   object:nil];
}