首页 > 解决方案 > iOS,UIScrollView 中的第一页启用横向,第二页禁用横向

问题描述

有一个 UIScrollView,它有两页,UIScrollView 中的第一页我想启用横向,UIScrollView 中的第二页我想禁用横向,现在我使用下面的代码,但它不起作用完善后,第二页先横屏再竖屏:

- (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"];
    }
}

我怎样才能做到这一点?每一个答案将不胜感激。

标签: iosobjective-corientationlandscape

解决方案


问题已解决。只需看下面的代码。

AppDelegate.h 文件:

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

AppDelegate.m 文件:

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

你的 UIViewController.m 文件:

- (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;
        }
    }
}

推荐阅读