Search code examples
iosobjective-corientationlandscape

iOS, first page in the UIScrollView enable landscape orientation and the second page disable the landscape orientation


There are one UIScrollView and it has two pages, the first page in the UIScrollView I want to enable landscape orientation and the second page in the UIScrollView I want to disable the landscape orientation,now I am use this code as below,but it work not perfected,the second page will horizontal screen and then portrait screen:

- (void)viewWillAppear:(BOOL)animated {
    [ITNotificationCenter addObserver:self selector:@selector(handleDeviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)handleDeviceOrientationDidChange{
    if(isFirstPage){
        //landscape code
    } else {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
}

How can I achieve this? Every answer will be appreciated.


Solution

  • The problem has been solved. Just see the code as below.

    AppDelegate.h file:

    @property (nonatomaic, assign, getter=isNeedRotation) BOOL needRotation;
    

    AppDelegate.m file:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (self.isNeedRotation) {
            return UIInterfaceOrientationMaskPortrait;
        }
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    

    yourUIViewController.m file:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView == self.mainScrollView) {
            AppDelegate *appDelegate = (AppDelegate *)ITUIApplicationDelegate;
            if (scrollView.contentOffset.x < screen_width) {
                appDelegate.needRotation = YES;
            } else {
                appDelegate.needRotation = NO;
            }
        }
    }